## 720. Longest Word in Dictionary

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.

If there is no answer, return the empty string.

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:

• All the strings in the input will only contain lowercase letters.
• The length of words will be in the range [1, 1000].
• The length of words[i] will be in the range [1, 30].

• b'
\n\n

#### Approach #1: Brute Force [Accepted]

\n

Intuition

\n

For each word, check if all prefixes word[:k] are present. We can use a Set structure to check this quickly.

\n

Algorithm

\n

Whenever our found word would be superior, we check if all it\'s prefixes are present, then replace our answer.

\n

Alternatively, 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.

\n

Python

\n
class 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
\n

Alternate Implementation

\n
class 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
\n

Java

\n
class 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
\n

Alternate Implementation

\n
class 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
\n

Complexity Analysis

\n
\n
• \n

Time complexity : , where is the length of words[i]. Checking whether all prefixes of words[i] are in the set is .

\n
• \n
• \n

Space complexity : to create the substrings.

\n
• \n
\n
\n

#### Approach #2: Trie + Depth-First Search [Accepted]

\n

Intuition

\n

As prefixes of strings are involved, this is usually a natural fit for a trie (a prefix tree.)

\n

Algorithm

\n

Put 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.

\n

In Python, we showcase a method using defaultdict, while in Java, we stick to a more general object-oriented approach.

\n

Python

\n
class 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
\n

Java

\n
class 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
\n

Complexity Analysis

\n
\n
• Time Complexity: , where is the length of words[i]. This is the complexity to build the trie and to search it.
• \n
\n

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.

\n
\n
• Space Complexity: , the space used by our trie.
• \n
\n
\n

Analysis written by: @awice.

\n
'