[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/feixiangcode/algorithm/master/Week_04/id_119/LeetCode_720_119.cpp [Back]  [Original]

class Solution {
public:
    string longestWord(vector& words) {
        string res = "";
        unordered_set s;
        sort(words.begin(), words.end());
        for (string word:words) {
            if (word.size() == 1 || s.count(word.substr(0, word.size() - 1))) {
                res = word.size() > res.size() ? word : res;
                s.insert(word);
            }
        }
        return res;
    }
};

Web Proxy Viewer  |  New URL  |  Original Page