Java || Trie || 8ms || O(words.length*words[i].length)
\n    public class Trie {\n\n\t\tprivate class Node {\n\n\t\t\tchar data;\n\t\t\tNode[] children;\n\t\t\tboolean isTerminal;\n\n\t\t\tpublic Node(char data, boo
\n    public class Trie {\n\n\t\tprivate class Node {\n\n\t\t\tchar data;\n\t\t\tNode[] children;\n\t\t\tboolean isTerminal;\n\n\t\t\tpublic Node(char data, boolean isTerminal) {\n\t\t\t\tthis.data = data;\n\t\t\t\tthis.isTerminal = isTerminal;\n\t\t\t\tthis.children = new Node[26];\n\t\t\t}\n\n\t\t}\n\n\t\tprivate Node root;\n\n\t\t// O(1)\n\t\tpublic Trie() {\n\t\t\tthis.root = new Node(\'\\0\', false);\n\t\t}\n\n\t\t// O(word.length)\n\t\tpublic void addWord(String word) {\n\t\t\tNode curr = root;\n\t\t\tfor (char ch : word.toCharArray()) {\n\t\t\t\tNode temp = curr.children[ch - \'a\'];\n\t\t\t\tif (temp == null) {\n\t\t\t\t\ttemp = new Node(ch, false);\n\t\t\t\t\tcurr.children[ch - \'a\'] = temp;\n\t\t\t\t}\n\t\t\t\tcurr = temp;\n\t\t\t}\n\t\t\tcurr.isTerminal = true;\n\t\t}\n\n\t\t// O(word.length)\n\t\tpublic boolean searchWord(String word) {\n\t\t\tNode curr = root;\n\t\t\tfor (char ch : word.toCharArray()) {\n\t\t\t\tNode temp = curr.children[ch - \'a\'];\n\t\t\t\tif (!temp.isTerminal) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\tcurr = temp;\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\n\t}\n\n\tString ans = "";\n\n\t// O(words.length*words[i].length) \n\tpublic String longestWord(String[] words) {\n\t\tTrie trie = new Trie();\n\t\tfor (String word : words)\n\t\t\ttrie.addWord(word);\n\t\tfor (String word : words) {\n\t\t\tif (trie.searchWord(word)) {\n\t\t\t\tif (word.length() > ans.length())\n\t\t\t\t\tans = word;\n\t\t\t\telse if (word.length() == ans.length())\n\t\t\t\t\tans = (word.compareTo(ans) < 0) ? word : ans;\n\t\t\t}\n\t\t}\n\t\treturn ans;\n\t}\n
[]