Java Trie
\nclass Solution {\n    TrieNode root = new TrieNode(\' \');\n    String result = "";\n    public String longestWord(String[] words) {\n        for(String word:
```\nclass Solution {\n    TrieNode root = new TrieNode(\' \');\n    String result = "";\n    public String longestWord(String[] words) {\n        for(String word:words) insert(word);\n        for(String word:words) \n            if(search(word))\n                if(word.length()>result.length()|| word.length() == result.length() && word.compareTo(result)<0) result = word;\n        return result;\n    }\n    \n    private void insert(String word) {\n        TrieNode currentNode = root;\n        for(int i=0;i<word.length();i++) {\n            if (currentNode.children.get(word.charAt(i)) == null) {\n                currentNode.children.put(word.charAt(i), new TrieNode(word.charAt(i)));\n            }\n            currentNode = currentNode.children.get(word.charAt(i));\n        }\n        currentNode.isEndOfWord = true;\n    }\n    \n    private boolean search(String word) {\n        TrieNode currentNode = root;\n        for(int i=0;i<word.length();i++) {\n            currentNode = currentNode.children.get(word.charAt(i));\n            if (currentNode == null || !currentNode.isEndOfWord) return false;\n        }\n        return currentNode.isEndOfWord;\n    }\n}\nclass TrieNode {\n    char ch;\n    Map<Character, TrieNode> children;\n    boolean isEndOfWord;\n    \n    public TrieNode(char ch) {\n        this.ch = ch;\n        children = new HashMap<>();\n    }\n}\n```
[]