Given a list of strings words
representing an English Dictionary, find the longest word in words
that can be built one character at a time by other words in words
. If there is more than one possible answer, return the longest word with the smallest lexicographical order.
Example 1:
Input: words = ["w","wo","wor","worl", "world"] Output: "world" Explanation: The word "world" can be built one character at a time by "w", "wo", "wor", and "worl".
Example 2:
Input: words = ["a", "banana", "app", "appl", "ap", "apply", "apple"] Output: "apple" Explanation: Both "apply" and "apple" can be built from other words in the dictionary. However, "apple" is lexicographically smaller than "apply".
Note:
words
will be in the range [1, 1000]
.words[i]
will be in the range [1, 30]
.Intuition
\nFor each word, check if all prefixes word[:k] are present. We can use a Set
structure to check this quickly.
Algorithm
\nWhenever our found word would be superior, we check if all it\'s prefixes are present, then replace our answer.
\nAlternatively, we could have sorted the words beforehand, so that we know the word we are considering would be the answer if all it\'s prefixes are present.
\nPython
\nclass Solution(object):\n def longestWord(self, words):\n ans = ""\n wordset = set(words)\n for word in words:\n if len(word) > len(ans) or len(word) == len(ans) and word < ans:\n if all(word[:k] in wordset for k in xrange(1, len(word))):\n ans = word\n\n return ans\n
Alternate Implementation
\nclass Solution(object):\n def longestWord(self, words):\n wordset = set(words)\n words.sort(key = lambda c: (-len(c), c))\n for word in words:\n if all(word[:k] in wordset for k in xrange(1, len(word))):\n return word\n\n return ""\n
Java
\nclass Solution {\n public String longestWord(String[] words) {\n String ans = "";\n Set<String> wordset = new HashSet();\n for (String word: words) wordset.add(word);\n for (String word: words) {\n if (word.length() > ans.length() ||\n word.length() == ans.length() && word.compareTo(ans) < 0) {\n boolean good = true;\n for (int k = 1; k < word.length(); ++k) {\n if (!wordset.contains(word.substring(0, k))) {\n good = false;\n break;\n }\n }\n if (good) ans = word;\n } \n }\n return ans;\n }\n}\n
Alternate Implementation
\nclass Solution {\n public String longestWord(String[] words) {\n Set<String> wordset = new HashSet();\n for (String word: words) wordset.add(word);\n Arrays.sort(words, (a, b) -> a.length() == b.length()\n ? a.compareTo(b) : b.length() - a.length());\n for (String word: words) {\n boolean good = true;\n for (int k = 1; k < word.length(); ++k) {\n if (!wordset.contains(word.substring(0, k))) {\n good = false;\n break;\n }\n }\n if (good) return word;\n }\n\n return "";\n }\n}\n
Complexity Analysis
\nTime complexity : , where is the length of words[i]
. Checking whether all prefixes of words[i]
are in the set is .
Space complexity : to create the substrings.
\nIntuition
\nAs prefixes of strings are involved, this is usually a natural fit for a trie (a prefix tree.)
\nAlgorithm
\nPut every word in a trie, then depth-first-search from the start of the trie, only searching nodes that ended a word. Every node found (except the root, which is a special case) then represents a word with all it\'s prefixes present. We take the best such word.
\nIn Python, we showcase a method using defaultdict, while in Java, we stick to a more general object-oriented approach.
\nPython
\nclass Solution(object):\n def longestWord(self, words):\n Trie = lambda: collections.defaultdict(Trie)\n trie = Trie()\n END = True\n\n for i, word in enumerate(words):\n reduce(dict.__getitem__, word, trie)[END] = i\n\n stack = trie.values()\n ans = ""\n while stack:\n cur = stack.pop()\n if END in cur:\n word = words[cur[END]]\n if len(word) > len(ans) or len(word) == len(ans) and word < ans:\n ans = word\n stack.extend([cur[letter] for letter in cur if letter != END])\n\n return ans\n
Java
\nclass Solution {\n public String longestWord(String[] words) {\n Trie trie = new Trie();\n int index = 0;\n for (String word: words) {\n trie.insert(word, ++index); //indexed by 1\n }\n trie.words = words;\n return trie.dfs();\n }\n}\nclass Node {\n char c;\n HashMap<Character, Node> children = new HashMap();\n int end;\n public Node(char c){\n this.c = c;\n }\n}\n\nclass Trie {\n Node root;\n String[] words;\n public Trie() {\n root = new Node(\'0\');\n }\n\n public void insert(String word, int index) {\n Node cur = root;\n for (char c: word.toCharArray()) {\n cur.children.putIfAbsent(c, new Node(c));\n cur = cur.children.get(c);\n }\n cur.end = index;\n }\n\n public String dfs() {\n String ans = "";\n Stack<Node> stack = new Stack();\n stack.push(root);\n while (!stack.empty()) {\n Node node = stack.pop();\n if (node.end > 0 || node == root) {\n if (node != root) {\n String word = words[node.end - 1];\n if (word.length() > ans.length() ||\n word.length() == ans.length() && word.compareTo(ans) < 0) {\n ans = word;\n }\n }\n for (Node nei: node.children.values()) {\n stack.push(nei);\n }\n }\n }\n return ans;\n }\n}\n
Complexity Analysis
\nwords[i]
. This is the complexity to build the trie and to search it.If we used a BFS instead of a DFS, and ordered the children in an array, we could drop the need to check whether the candidate word at each node is better than the answer, by forcing that the last node visited will be the best answer.
\nAnalysis written by: @awice.
\n