C# quick and easy to write solution using HashSet
Sept. 27, 2018\nIt is easy level algorithm tagged by HashTable. There is a very smart solution to sort the words, therefore any word will be listed after its pr
Sept. 27, 2018\nIt is easy level algorithm tagged by HashTable. There is a very smart solution to sort the words, therefore any word will be listed after its prefix word. \n\nTwo things are very important to come out the solution using HashSet. \n1. Sort the words in ascending order. \n2. If the word "world" is visited, then "worl" must have been visited before if worl is in dictionary; think recursively, only need to check "worl" is in visited dictionary and also satisfy the constraint "all prefixs are in dictionary".\n\nI will make it more clear in English later on. I like to share my C# code. \n\n```\npublic class Solution {\n    public string LongestWord(string[] words) {\n            if(words == null)\n                return null;\n\n            Array.Sort(words); \n\n            // word with prefixes in the dictionary\n            var visitedPrefix = new HashSet<string>(); \n\n            string maximumWord = "";\n\n            foreach(var word in words)\n            {\n                var length = word.Length;\n\n                // think carefully about this reason using w, wo, wor, word\n                if(length == 1 || visitedPrefix.Contains(word.Substring(0, length - 1)))\n                {\n                    // keep lexico order - apple will be checked before apply\n                    maximumWord = word.Length > maximumWord.Length? word : maximumWord; \n                    visitedPrefix.Add(word); \n                }\n            }\n\n            return maximumWord;\n    }\n}\n```\n
[]