✅✅Java Solution Using Tries✅✅
Code\n\nclass TrieNode{\n  TrieNode[] children;\n  boolean end;\n  TrieNode(){\n    children = new TrieNode[26];\n    end = false;\n  }\n}\nclass Trie{\n  publi
# Code\n```\nclass TrieNode{\n  TrieNode[] children;\n  boolean end;\n  TrieNode(){\n    children = new TrieNode[26];\n    end = false;\n  }\n}\nclass Trie{\n  public static TrieNode root;\n  Trie(){\n    root = new TrieNode();\n  }\n  public static void insert(String word){\n    TrieNode curr = root;\n    for(int i=0;i<word.length();i++){\n      char ch = word.charAt(i);\n      if(curr.children[ch-\'a\']==null){\n        curr.children[ch-\'a\'] = new TrieNode();\n      }\n      curr = curr.children[ch-\'a\'];\n    }\n    curr.end = true;\n  }\n  public static boolean search(String word){\n    TrieNode curr = root;\n    for(int i=0;i<word.length();i++){\n      char ch = word.charAt(i);\n      if(curr.children[ch-\'a\']==null) return false;\n      curr = curr.children[ch-\'a\'];\n    }\n    return curr.end;\n  }\n}\nclass Solution {\n    public String longestWord(String[] a) {\n        Trie t = new Trie();\n        for(String s:a){\n          t.insert(s);\n        }\n        String answer = "";\n        for(String s:a){\n          boolean flag = false;\n          for(int i=0;i<s.length();i++){\n            if(!t.search(s.substring(0,i+1))){\n              flag = false;\n              break;\n            }\n            else flag = true;\n          }\n          if(flag){\n            if(answer.length()<s.length()) answer = s;\n            else if(answer.length()==s.length()){\n              if(answer.compareTo(s)>0) answer = s;\n              else continue;\n            }\n          }\n        }\n        return answer;\n    }\n}\n```
['Trie', 'Java']