C++ Trie Solution with Explanation
When using Trie I personally don\'t understand why the question has been labelled as easy.\nAnyways upvote if you like the explanation.\n\n class Trie{\n       
When using Trie I personally don\'t understand why the question has been labelled as easy.\nAnyways upvote if you like the explanation.\n```\n class Trie{\n        public:\n\t\t//Initializing Trie\n        Trie* child[26]={NULL};\n        bool end;\n    };\nclass Solution {\npublic:\n\t//Stores the final answer string\n    string ans="";\n\t\n\t//Inserts the ith word int the Trie\n    void insert(string w, Trie* root){\n\t\n\t\t//Stores the current word only if it\'s prefix is present\n        string s="";\n        int n=w.length();\n        Trie* cur=root;\n\t\t\n        for(int i=0;i<n;i++){\n\t\t\tif(i==n-1)s+=w[i]; //stores the first char or the last char in the temporary string s\n            int k=w[i]-\'a\';\n            if(cur->child[k]==NULL){\n                if(i!=n-1)break; //if the char is not present and it isn\'t the last char of the word\n\t\t\t\t//then no need to insert it. EX: {"a","ad","adpf"} we won\'t be inserting adpf \n\t\t\t\t//as there is no "adp"\n                cur->child[k]=new Trie(); //creates a new child trie\n            }\n            else s+=w[i]; //if prefix is present we add it in the string s\n            cur=cur->child[k];\n        }\n\t\t\n        if(s.length()>ans.length())ans=s; //if cur string s length > ans then update ans\n        else if(s.length()==ans.length() && s<ans)ans=s; //if both lengths are same \n\t\t//check for lexigraphical order and update accordingly\n        cur->end=true;\n    }\n   \n    string longestWord(vector<string>& words) {\n        int n=words.size();\n        sort(begin(words),end(words));\n        Trie *root =new Trie();\n        for(int i=0;i<n;i++){\n\t\t\t//if consecutive words are same no need to call insert function for duplicate words\n            if(i+1<n && words[i]==words[i+1])continue;\n            insert(words[i],root);\n        }\n\t\t//returns the globally declared ans string\n        return ans;\n    }\n};\n```
['Trie', 'C', 'C++']