-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path792.cpp
More file actions
41 lines (41 loc) · 1.27 KB
/
Copy path792.cpp
File metadata and controls
41 lines (41 loc) · 1.27 KB
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
40
41
class Solution {
public:
int numMatchingSubseq(string S, vector<string> &words) {
unordered_map<char, vector<int>> sub;
int total = 0;
for (int i = 0; i < S.size(); ++i) {
if (sub.find(S[i]) == sub.end())
sub[S[i]] = vector<int>();
sub[S[i]].push_back(i);
}
for (int i = 0; i < words.size(); ++i) {
auto &word = words[i];
int j = 0, current_sub = -1;
bool exist = true;
while (j < word.size()) {
auto c = word[j];
if (sub[c].size() == 0) {
exist = false;
break;
}
int x = 0, y = sub[c].size(), mid = 0;
while (x < y) {
mid = x + (y - x) / 2;
if (sub[c][mid] > current_sub)
y = mid;
else if (sub[c][mid] <= current_sub)
x = mid + 1;
}
if (x == sub[c].size()) {
exist = false;
break;
} else
current_sub = sub[c][y];
++j;
}
if (exist)
++total;
}
return total;
}
};