Simple Java solution with custom comparator
\nSort the array by length. If the length is equal, sort lexicographically.\n\nclass Solution {\n    public String longestWord(String[] words) {\n    Arrays.sor
\nSort the array by length. If the length is equal, sort lexicographically.\n```\nclass Solution {\n    public String longestWord(String[] words) {\n    Arrays.sort(words,new Comparator<String>()\n                {\n                    public int compare(String o1,String o2)\n                    {\n                         if(o1.length() < o2.length()){\n                            return 1;\n                        }else if(o1.length() > o2.length()){\n                             return -1;\n                         }\n                            return o1.compareTo(o2);\n                    }\n                });\n    Map<String, Boolean> map = new HashMap();\n    for (int i = 0 ; i < words.length; i++)\n    {\n        map.put(words[i], true);\n    }\n    for (int i = 0 ; i < words.length; i++)\n    {\n        String longWord = words[i];\n        String subString = "";\n        boolean valid = false;\n        for (int j= 0 ; j < longWord.length(); j++)\n        {\n            subString = subString + Character.toString(longWord.charAt(j));\n            if (null != map.get(subString))\n            {\n                valid = true;\n            }\n            else\n            {\n                valid = false;\n                break;\n            }\n        }\n        if (valid == true)\n        {\n            return longWord;\n        }\n    }\n    return "";\n}\n}\n```
[]