Skip to content

Commit e916d27

Browse files
committed
Time: 184 ms (53.80%), Space: 49.9 MB (40.78%) - LeetHub
1 parent 9db301f commit e916d27

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
int numMatchingSubseq(string str, vector<string>& words) {
4+
vector<vector<int>> vec(26);
5+
int n = str.size();
6+
7+
for(int i = 0; i < n; i++){
8+
vec[str[i] - 'a'].push_back(i);
9+
}
10+
11+
int cnt = 0;
12+
13+
for(auto w: words){
14+
int curr_idx = -1;
15+
bool flag = true;
16+
17+
for(auto c: w){
18+
int idx = upper_bound(vec[c - 'a'].begin(), vec[c - 'a'].end(), curr_idx) - vec[c - 'a'].begin();
19+
if(idx >= (int)vec[c - 'a'].size()){
20+
flag = false;
21+
break;
22+
}
23+
curr_idx = vec[c - 'a'][idx];
24+
}
25+
26+
if(flag)
27+
cnt++;
28+
}
29+
30+
return cnt;
31+
}
32+
};

0 commit comments

Comments
 (0)