11392 . Is Subsequence
22
3- class Solution {
3+ class Solution
4+ {
45public:
56 int k = 0 ;
6- bool isSubsequence (string s, string t) {
7- for (int i = 0 ; i < t.length (); i++){
8- if (t[i] == s[k])
7+ bool isSubsequence (string s, string t)
8+ {
9+ for (int i = 0 ; i < t.length (); i++)
10+ {
11+ if (t[i] == s[k])
912 k++;
1013 }
11-
12- if (k == s.length ()){
14+
15+ if (k == s.length ())
16+ {
1317 return true ;
1418 }
15-
16- return false ;
17-
19+
20+ return false ;
21+ }
22+ };
23+ /*
24+ BruteForce Approach
25+
26+ Just we need to compare both string by traversing
27+ if t[i] == s[i] , we will increase the count.
28+ if cnt == s.length() this means t is the subsequence of s
29+ As, it contains alll the characters.
30+ CODE
31+ */
32+ class Solution
33+ {
34+ public:
35+ bool isSubsequence (string s, string t)
36+ {
37+
38+ int j = 0 ; // For index of str1 (or subsequence
39+
40+ // Traverse str2 and str1, and
41+ // compare current character
42+ // of str2 with first unmatched char
43+ // of str1, if matched
44+ // then move ahead in str1
45+ for (int i = 0 ; i < t.length () && j < s.length (); i++)
46+ if (s[j] == t[i])
47+ j++;
48+
49+ // If all characters of str1 were found in str2
50+ return (j == s.length ());
51+ }
52+ };
53+
54+ /*
55+ RECURSIVE IMPLEMENTATION
56+
57+ The idea is simple, we traverse both strings from one side to another side
58+ (say from rightmost character to leftmost).
59+ If we find a matching character, we move ahead in both strings.
60+ Otherwise, we move ahead only in str2.
61+ CODE
62+ */
63+
64+ class Solution
65+ {
66+ public:
67+ bool isSubs (string &s, string &t, int m, int n)
68+ {
69+ if (m == 0 )
70+ return true ;
71+ if (n == 0 )
72+ return false ;
73+
74+ // If last characters of two
75+ // strings are matching
76+ if (s[m - 1 ] == t[n - 1 ])
77+ return isSubs (s, t, m - 1 , n - 1 );
78+
79+ // If last characters are
80+ // not matching
81+ return isSubs (s, t, m, n - 1 );
82+ }
83+
84+ bool isSubsequence (string s, string t)
85+ {
86+
87+ if (isSubs (s, t, s.length (), t.length ()))
88+ return true ;
89+
90+ return false ;
91+ }
92+ };
93+
94+ /*
95+ MEMOIZATION TECHNIQUE
96+
97+ Here the idea is to check whether the size of the longest common subsequence is equal to the size of str1.
98+ If it’s equal it means there is a subsequence that exists in str2.
99+ CODE
100+ */
101+
102+ class Solution
103+ {
104+ public:
105+ int t[1001 ][1001 ];
106+
107+ // returns the length of longest common subsequence
108+ int isSubs (string &s1, string &s2, int i, int j, vector<vector<int >> &t)
109+ {
110+ if (i == 0 || j == 0 )
111+ return 0 ;
112+ if (t[i][j] != -1 )
113+ return t[i][j];
114+ if (s1[i - 1 ] == s2[j - 1 ])
115+ return t[i][j] = 1 + isSubs (s1, s2, i - 1 , j - 1 , t);
116+ else
117+ return t[i][j] = isSubs (s1, s2, i, j - 1 , t);
118+ }
119+
120+ bool isSubsequence (string s1, string s2)
121+ {
122+ int m = s1.length ();
123+ int n = s2.length ();
124+
125+ // intialising dp matrix with -1
126+
127+ if (m > n)
128+ return false ;
129+
130+ vector<vector<int >> t (m + 1 , vector<int >(n + 1 , -1 ));
131+
132+ if (isSubs (s1, s2, m, n, t) == m)
133+ return true ;
134+ return false ;
18135 }
19136};
0 commit comments