Java || HashSet + StringBuilder || 5ms || beats 99.54% || O(words.length*words[i].length)
\n    public String longestWord(String[] words) {\n\n\t\tString ans = "";\n\t\tHashSet set = new HashSet();\n\t\tfor (String word : words)\n\t\t\tset.add(word);
\n    public String longestWord(String[] words) {\n\n\t\tString ans = "";\n\t\tHashSet<String> set = new HashSet<String>();\n\t\tfor (String word : words)\n\t\t\tset.add(word);\n\n\t\tfor (String word : words) {\n\t\t\tif ((word.length() > ans.length()) || (word.length() == ans.length() && word.compareTo(ans) < 0)) {\n\t\t\t\tStringBuilder sb = new StringBuilder();\n\t\t\t\tboolean good = true;\n\t\t\t\tfor (int i = 0; i < word.length() - 1; i++) {\n\t\t\t\t\tsb.append(word.charAt(i));\n\t\t\t\t\tif (!set.contains(sb.toString())) {\n\t\t\t\t\t\tgood = false;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (good)\n\t\t\t\t\tans = word;\n\t\t\t}\n\t\t}\n\t\treturn ans;\n\t}\n
[]