By using Trie  simple
\n\n# Code\n\nclass Solution {\n    public: \n    struct Node{\n        Node* children[26];\n        bool isend = false;\n    };\npublic:\n     Node* root = new
\n\n# Code\n```\nclass Solution {\n    public: \n    struct Node{\n        Node* children[26];\n        bool isend = false;\n    };\npublic:\n     Node* root = new Node();\n     void insert(string &word){\n         Node*node=root;\n         for(int i=0; i<word.size(); i++){\n             int index=word[i]-\'a\';\n             if(!node->children[index]){\n                 node->children[index]=new Node();\n             }\n             node=node->children[index];\n         }\n         node->isend=true;\n     }\n      bool isValid(string &word){\n             Node * node = root;\n                int n = word.size();\n                   for(int i =0;i<n;i++){    \n                       int index=word[i]-\'a\';   \n                    node = node->children[index];\n                    if(node->isend == false) return false;       \n                }\n       return true;\n    }\n    string longestWord(vector<string>& words) {\n        string ans ="";\n        for(auto w: words){\n            insert(w);\n        }  \n        for(auto i: words){\n            if(isValid(i)){\n                if(i.length() > ans.length()){\n                    ans =i;\n                }else if(i.length() == ans.length()){\n                    ans = min(i, ans);\n                }\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']