【排序+lamda表达式】524. 通过删除字母匹配到字典里最长单词

class Solution {
public:
    string findLongestWord(string s, vector<string>& dictionary) {
        vector<string> match_s;
        for(auto dict_s : dictionary){
            int j = 0;
            for(int i=0;i<s.size();++i)
                if(s[i] == dict_s[j])
                    j ++;
            if (j == dict_s.size())
                match_s.push_back(dict_s);
        }
        sort(match_s.begin(),match_s.end(), [](auto a, auto b){  
            return (a.size() > b.size() || (a.size() == b.size() && a < b));
        });
        return match_s.size() == 0 ? "" : match_s[0];   
    }
};
posted @ 2022-02-28 20:06  fwx  阅读(27)  评论(0)    收藏  举报