You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given a string s and a string t, check if s is subsequence of t.
3
+
A subsequence of a string is a new string which is formed from the original string by deleting some(can be none) of the characters without disturbing the relative positions of the remaining characters.
4
+
(ie, "ace" is a subsequence of "abcde" while "aec" is not).
5
+
*/
6
+
#include<bits/stdc++.h>
7
+
usingnamespacestd;
8
+
boolisSubsequence(string s, string t) {
9
+
int i, j = 0;
10
+
if (s.length() == 0) // empty string is always a subsequence
11
+
returntrue;
12
+
char c = s[0]; //c stores first character of s
13
+
if (t.length() < s.length()) // a string s containing less characters than t cannot be its subsequence
14
+
returnfalse;
15
+
for (i = 0; i < t.length(); i++)
16
+
{
17
+
if (t[i] == c)
18
+
{
19
+
j++; //it moves the iterator to the next character of s
20
+
if (j == s.length()) //if the iterator has reached the limit of s , it means it is present as subsequence
21
+
returntrue;
22
+
c = s[j];
23
+
}
24
+
}
25
+
returnfalse; //if the conditions are not satisified above, then s is not a subsequence of t
26
+
}
27
+
intmain()
28
+
{
29
+
string s, t;
30
+
cin >> s >> t;
31
+
cout << boolalpha; //to display true for 1 and false for 0
0 commit comments