-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis-subsequence.cpp
44 lines (40 loc) · 1.09 KB
/
is-subsequence.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
36
37
38
39
40
41
42
43
class Solution {
public:
bool isSubsequence(string s, string t) {
unordered_map<char,vector<int>> um;
for(int i=0;i<t.size();i++){
um[t[i]].push_back(i);
}
int pre=-1;
for(int i=0;i<s.size();i++){
char &c=s[i];
auto it=upper_bound(um[c].begin(),um[c].end(),pre);
if(it==um[c].end()) return false;
pre=*it;
}
return true;
}
bool isSubsequence2(string s, string t) {
unordered_map<char,vector<int>> um;
for(int i=0;i<t.size();i++){
char &c=t[i];
um[c].emplace_back(i);
}
int pre=-1;
for(int i=0;i<s.size();i++){
char &c=s[i];
vector<int> tmp=um[c];
auto it=upper_bound(tmp.begin(),tmp.end(),pre);
if(it==tmp.end()) return false;
pre=*it;
}
return true;
}
bool isSubsequence1(string s, string t) {
int i=0;
for(int j=0;j<t.size();j++){
if(t[j]==s[i]) i++;
}
return i==s.size();
}
};