[Java/C++] Clean Code
Sort the words alphabetically, therefore shorter words always comes before longer words;\n2. Along the sorted list, populate the words that can be built;\n3. An
1. Sort the words alphabetically, therefore shorter words always comes before longer words;\n2. Along the sorted list, populate the words that can be built;\n3. Any prefix of a word must comes before that  word.\n\n**Java**\n```\nclass Solution {\n    public String longestWord(String[] words) {\n        Arrays.sort(words);\n        Set<String> built = new HashSet<String>();\n        String res = "";\n        for (String w : words) {\n            if (w.length() == 1 || built.contains(w.substring(0, w.length() - 1))) {\n                res = w.length() > res.length() ? w : res;\n                built.add(w);\n            }\n        }\n        return res;\n    }\n}\n```\n**C++**\n```\nclass Solution {\npublic:\n    string longestWord(vector<string>& words) {\n        sort(words.begin(), words.end());\n        unordered_set<string> built;\n        string res;\n        for (string w : words) {\n            if (w.size() == 1 || built.count(w.substr(0, w.size() - 1))) {\n                res = w.size() > res.size() ? w : res;\n                built.insert(w);\n            }\n        }\n        return res;\n    }\n};\n```
[]