Leetcode 524. 通过删除字母匹配到字典里最长单词
思路:判断子序列
O ( n ) O(n) O(n)做法判断子序列,每一次比较答案,保存字典序最小的字符串
class Solution {
public:
bool check(string& a, string& b) {
int i = 0, j = 0;
while (i < a.size() && j < b.size()) {
if (a[i] == b[j]) i ++ ;
j ++ ;
}
return i == a.size();
}
string findLongestWord(string s, vector<string>& dictionary) {
string res;
for (auto& str : dictionary) {
if (check(str, s)) {
if (res.empty() || res.size() < str.size() || res.size() == str.size() && str < res)
res = str;
}
}
return res;
}
};