C++ O(N) Solution using set
\n# Complexity\n- Time complexity: O(N)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(N)\n Add your space complexity here, e.g. O(n) \n\n
\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    // bool check()\n    string longestWord(vector<string>& words) {\n        string ans="";\n        unordered_set<string> s(words.begin(),words.end());\n        for(auto x:words){\n            int i=0;\n            while(i<x.size()){\n                if(s.find(x.substr(0,i+1))!=s.end())\n                    i++;\n                else break;\n            }\n            if(i==x.size()){\n                if(x.size()>ans.size()){\n                    ans=x;\n                }\n                if(x.size()==ans.size()){\n                    ans=min(ans,x);\n                }\n            }\n        }\n        return ans;\n    }\n};\n```\n*if u like pls upvote:)*
['Hash Table', 'C++']