-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisSubsequence.cpp
More file actions
50 lines (37 loc) · 1.13 KB
/
isSubsequence.cpp
File metadata and controls
50 lines (37 loc) · 1.13 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
42
43
44
45
46
47
48
49
50
#include"import.h"
using namespace std;
class Solution {
public:
bool isSubsequence(string s, string t) {
int lenS = s.length();
int lenT = t.length();
if(!lenT) return true;
if(!lenS) return false;
unordered_map<char, vector<int> > mymap;
for(int i =0; i< lenT; i++){
mymap[t[i]].push_back(i);
}
//constructed the map Now check the subsequence
int prevIndex = -1;
for(int i =0; i<lenS; i++){
if(mymap[s[i]].empty()) return false;
int newIndex=-1;
int cnt=0;
while(cnt <mymap[s[i]].size() ){
newIndex = mymap[s[i]][cnt++];
if(newIndex > prevIndex){
prevIndex = newIndex;
break;
}
}
if(newIndex == -1 || newIndex < prevIndex)
return false;
}
return true;
}
};
int main() {
Solution obj;
cout << obj.isSubsequence("abc", "ahgdcb");
return 0;
}