Skip to content

Commit 63d64c3

Browse files
Merge pull request #60 from PawanJaiswal0843/master
392.is-subsequence.cpp
2 parents 7d9ca73 + 1c459ff commit 63d64c3

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

392.is-subsequence.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// 392. Is Subsequence
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
void solve(string &t, vector<string> &ans, int index, string &temp) {
6+
7+
if(index == t.size()) {
8+
ans.push_back(temp);
9+
return;
10+
}
11+
12+
// exclude
13+
solve(t, ans, index+1, temp);
14+
15+
// inclusion
16+
temp.push_back(t[index]);
17+
solve(t, ans, index+1, temp);
18+
temp.pop_back();
19+
}
20+
21+
bool isSubsequence(string s, string t) {
22+
23+
vector<string> ans;
24+
string temp = "";
25+
26+
solve(t, ans, 0, temp);
27+
28+
for(int i=0; i<ans.size(); i++) {
29+
if(ans[i] == s) {
30+
return true;
31+
}
32+
}
33+
34+
return false;
35+
}
36+
37+
bool solve2(string s, string t, int i, int j) {
38+
39+
if(i >= s.size())
40+
return true;
41+
42+
if(i < s.size() && j >= t.size())
43+
return false;
44+
45+
if(s[i] == t[j])
46+
return solve(s, t, i+1, j+1);
47+
48+
else
49+
return solve(s, t, i, j+1);
50+
}
51+
52+
bool isSubsequence1(string s, string t) {
53+
return solve2(s, t, 0, 0);
54+
}
55+
int main() {
56+
57+
string s = "abc";
58+
string t = "ahbgdc";
59+
60+
cout << isSubsequence(s, t);
61+
62+
return 0;
63+
}

0 commit comments

Comments
 (0)