C++ using Trie (explained)
First store every word in the Trie and mark a boolean value isEnd = true, as this will indicate the ending of word. Now for every string in words, traverse Trie
First store every word in the Trie and mark a boolean value `isEnd = true`, as this will indicate the ending of word. Now for every string in words, traverse Trie and count the `isEnd` boolean value is the path. If the `count` equals the length of string that means we encounter all the possible prefix of the current word.\n\n```\nclass TrieNode {\npublic:\n    unordered_map<char, TrieNode*> mp;\n    bool isEnd;\n    \n    TrieNode() {\n        this->isEnd = false;\n    }\n};\n\nclass Solution {\npublic:\n    string longestWord(vector<string>& words) \n    {\n        TrieNode *root = new TrieNode();\n        for(string s: words) {\n            insert(root, s);\n        }\n        \n        string ans;\n        for(string s: words) \n        {\n            if(ans.size() > s.size()) continue;\n            if(search(root, s)) {\n                if(ans.size() == s.size()) ans = min(s, ans);\n                else ans = s;\n            }\n        }\n        return ans;\n    }\n    \n    void insert(TrieNode *root, string s)\n    {\n        TrieNode *curr = root;\n        for(char c: s)\n        {\n            if(curr->mp.find(c) == curr->mp.end()) {\n                curr->mp[c] = new TrieNode();\n            }\n            curr = curr->mp[c];\n        }\n        curr->isEnd = true;\n    }\n    \n    bool search(TrieNode *root, string s)\n    {\n        int count = 0;\n        TrieNode *curr = root;\n        for(char c: s)\n        {\n            curr = curr->mp[c];\n            if(curr->isEnd) ++count;\n        }\n        return count == s.size();\n    }\n};\n```
['C']