O(1) Space! NlogN Solution! [Python/C++]
this takes advantage of lexicographic ordering. i think the code is self explanatory.\n\nhad idea after reading wofainta\'s solution here: \n\nc++\n\nclass Solu
this takes advantage of lexicographic ordering. i think the code is self explanatory.\n\nhad idea after reading wofainta\'s solution [here](https://leetcode.com/problems/longest-word-in-dictionary/discuss/175385/C++-O(N-logN)-time-O(1)-space-solution): \n\nc++\n```\nclass Solution {\npublic:\n    string longestWord(vector<string>& words) {\n        string best, prefix;\n        sort(words.begin(), words.end());\n        for (string w: words) {\n            int sz = w.size();\n            if (w.substr(0, sz - 1) == prefix.substr(0, sz - 1)) {\n                prefix = w;\n                if (sz > best.size()) best = w;\n            }\n        }\n        return best;\n    }\n};\n```\npython\n```\nclass Solution(object):\n    def longestWord(self, words):\n        best = prefix = ""\n        for w in sorted(words):\n            if w[:len(w) - 1] == prefix[:len(w) - 1]:\n                prefix = w\n                best = max([best, w], key=len)\n        return best\n```
[]