-
Notifications
You must be signed in to change notification settings - Fork 0
/
524.hpp
39 lines (33 loc) · 869 Bytes
/
524.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef LEETCODE_524_HPP
#define LEETCODE_524_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <numeric>
#include <stack>
#include <string>
using namespace std;
class Solution {
public:
string findLongestWord(string s, vector<string> &d) {
sort(d.begin(), d.end(), [](const string &l, const string &r) -> bool {
return l.size() > r.size() || (l.size() == r.size() && l < r);
});
for (const string &word : d) {
int i = 0, j = 0;
for (; i < s.size() && j < word.size(); ++i) {
if (word[j] == s[i])
j++;
}
if (j == word.size()) {
return word;
}
}
return "";
}
};
#endif //LEETCODE_524_HPP