2 solutions Set and Trie
Solution 1: \nThis is a naive Solution checking all prefixs \n\nclass Solution {\npublic:\n    string longestWord(vector<string>& words) {\n        string ans =
Solution 1: \nThis is a naive Solution checking all prefixs \n```\nclass Solution {\npublic:\n    string longestWord(vector<string>& words) {\n        string ans = "";\n        set<string> st;\n        for(auto s : words)\n            st.insert(s);\n        \n        for(string word : words) {\n            if(word.length()>ans.length() || (word.length()==ans.length() && ans>word))  {\n                bool fine = 1;\n                for(int k=1;k<word.length();k++){\n                    if(st.find(word.substr(0,k)) == st.end()){\n                        fine = 0;\n                        break;\n                    }\n                }\n                if(fine) ans = word;\n            }\n        }\n        \n        return ans;\n    }\n};\n```\nSolution 2 :  Using trie data structure:\n```\nclass TrieNode{\n  public : \n    vector<TrieNode*> children;\n    bool isEnd;\n    \n    TrieNode(){\n        children.assign(26,NULL);\n        isEnd = 0;\n    }    \n};\n\nclass Trie{\n  public: \n    TrieNode* root;\n    Trie(){\n        root = new TrieNode();\n    }\n    void insert(string s){\n        TrieNode* curr = root;\n        for(int i=0;i<s.length();i++){\n            if(curr->children[s[i]-\'a\'] == NULL) \n                curr->children[s[i]-\'a\'] = new TrieNode();\n            curr = curr->children[s[i]-\'a\'];\n        }\n        curr->isEnd = true;\n    }\n    \n    bool present(string word){\n        TrieNode* curr = root;\n        for(int i=0;i<word.length();i++){\n            if(root ==curr || curr->isEnd) \n                curr = curr->children[word[i]-\'a\'];\n            else \n                return false;\n        }\n        return true;\n    }\n    \n    \n};\n\nclass Solution {\npublic:\n    string longestWord(vector<string>& words) {\n        Trie* trie = new Trie();\n        for(string s : words){\n            trie->insert(s);\n        }\n        string ans = "";\n        for(string word : words) {\n            if(trie->present(word) && word.length()>=ans.length()) {\n                ans = (ans.length() == word.length()) ? min(ans, word) : word;\n            }\n        }\n        return ans;\n    }\n};\n```
['Trie', 'C', 'C++']