class Solution {
public:
string findLongestWord(string s, vector<string>& dictionary) {
vector<string> res;
for (string x : dictionary) {
int j = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == x[j]) {
j++;
}
}
if (j == x.size()) {
res.push_back(x);
}
}
sort(res.begin(), res.end(), [](string a, string b) {
return a.size() > b.size() || (a.size() == b.size() && a < b);
});
return res.size() > 0 ? res[0] : "";
}
};
LeetCode #524 通过删除字母匹配到字典里最长单词
最新推荐文章于 2024-07-25 19:34:38 发布