-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy path0524.cpp
35 lines (33 loc) · 954 Bytes
/
0524.cpp
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
class Solution {
public:
string findLongestWord(string s, vector<string>& dictionary) {
const int n = s.size();
int f[n + 1][26];
memset(f[n], -1, sizeof(f[n]));
for (int i = n - 1; ~i; i--) {
for (int j = 0; j < 26; j++) {
if (s[i] == j + 97) f[i][j] = i;
else f[i][j] = f[i + 1][j];
}
}
string_view res = "";
for (auto& w : dictionary) {
bool match = true;
int j = 0;
for (auto& c : w) {
if (f[j][c - 97] == -1) {
match = false;
break;
}
j = f[j][c - 97] + 1;
}
if (match) {
if (res.size() < w.size() ||
(res.size() == w.size() && res > w)) {
res = w;
}
}
}
return string(res);
}
};