[C++] 2 solns, tries, hashset
\nclass Solution {\npublic:\n    string longestWord(vector<string>& words) \n    {\n        sort(words.begin(), words.end());\n        string result = "";\n    
```\nclass Solution {\npublic:\n    string longestWord(vector<string>& words) \n    {\n        sort(words.begin(), words.end());\n        string result = "";\n        unordered_set<string> allwords;\n        \n        for ( string word : words ) {\n            if ( word.length() == 1 || allwords.find(word.substr(0,word.length()-1)) != allwords.end() ) {\n                if ( word.length() > result.length() ) {\n                    result = word;\n                }\n                allwords.insert(word);\n            }\n        }\n        \n        return result;\n    }\n};\n\n```\n\n```\nclass Solution {\npublic:\n    class Node \n    {\n    public:\n        char val;\n        bool isend;\n        Node* children[26];\n        \n        Node( char ch ) {\n            val = ch;\n            isend = false;\n            for ( int i=0; i<26; i++ ) {\n                children[i] = NULL;\n            }\n        }\n    };\n    \n    Node* root = new Node(\' \');\n    string maxValidWord = "";\n    int maxValidWordSize = INT_MIN;\n    \n    void insertIntoTrie ( string word )\n    {\n        Node* cur = root;\n        for ( char ch : word ) {\n            if ( cur->children[ch-\'a\'] == NULL ) {\n                cur->children[ch-\'a\'] = new Node(ch);\n            }\n            cur = cur->children[ch-\'a\'];\n        }\n        cur->isend = true;\n    }\n    \n    void displayAllValidWords ( Node* root, bool valid, string word) \n    {\n        if ( root == NULL ) {\n            return;\n        }\n        \n        word = word + root->val;\n        \n        if ( root->val != \' \' && !root->isend ) {\n            valid = false;\n        }\n        \n        if ( valid ) {\n            int cursize = word.length();\n            if ( cursize > maxValidWordSize ) {\n                maxValidWordSize = word.length();\n                maxValidWord = word.substr(1);\n            }\n        }\n        else {\n            return;\n        }\n        \n        for ( int i=0; i<26; i++ ) {\n            displayAllValidWords(root->children[i], valid, word);\n        }\n    }\n    \n    string longestWord ( vector<string>& words ) \n    {\n        for ( string word : words ) {\n            insertIntoTrie(word);\n        }\n        displayAllValidWords(root, true, "");\n        return maxValidWord;\n    }\n};\n```
['Trie', 'C']