每日一题ing,今天是个easy题524. Longest Word in Dictionary through Deleting
class Solution {
public:
static bool cmp(string s1, string s2) {
if (s1.size() == s2.size()) {
return s1 < s2;
}
return s1.size() > s2.size();
}
string findLongestWord(string s, vector<string>& dictionary) {
sort(dictionary.begin(), dictionary.end(), cmp);
for (int i = 0; i < dictionary.size(); i++) {
int j = 0, k = 0;
int len1 = dictionary[i].size(), len2 = s.size();
while (j < len1 && k < len2) {
if (dictionary[i][j] == s[k]) {
if (j == len1 - 1) {
return dictionary[i];
}
j++; k++;
}
else {
k++;
}
}
}
return "";
}
};