By using map simple approach
\n# Code\n\nclass Solution {\npublic:\n    string longestWord(vector<string>& a) {\n        sort(a.begin(), a.end());\n         map<string, int>mp;\n\n         
\n# Code\n```\nclass Solution {\npublic:\n    string longestWord(vector<string>& a) {\n        sort(a.begin(), a.end());\n         map<string, int>mp;\n\n             mp[""]++;\n             string ans;\n\n          for(auto i:a){\n              \n              string temp=i.substr(0,i.size()-1);\n\n              if(mp[temp]!=0){\n                  if(ans.size()<i.size()){\n                      ans=i;\n                  }\n                  mp[i]++;\n              }\n          }\nreturn ans;\n    }\n};\n```
['C++']