【排序+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];
}
};

浙公网安备 33010602011771号