Given two strings s1 and s2, find if the first string is a Subsequence of the second string, i.e. if s1 is a subsequence of s2.Â
Examples :Â
Input: s1 = "AXY", s2 = "ADXCPY"
Output: true
Explanation: All characters of s1 are in s2 in the same orderInput: s1 = "AXY", s2 = "YADXCP"
Output: false
Explanation: All characters are present, but order is not same.Input: s1 = "gksrek", s2 = "geeksforgeeks"
Output: true
Recursion - O(n) Time and O(n) Space
We match last characters of the two strings s1 and s2 of lengths m and n
- If the last characters match, we call for m-1 and n-1
- Otherwise, we ignore the last character of s2 and therefore call for m and n-1.
Below is the Implementation of the above idea:
#include <iostream>
#include <string>
using namespace std;
bool isSubSeqRec(string& s1, string& s2, int m, int n){
// Base Cases
if (m == 0)
return true;
if (n == 0)
return false;
// If last characters match
if (s1[m - 1] == s2[n - 1])
return isSubSeqRec(s1, s2, m - 1, n - 1);
// If last characters don't match
return isSubSeqRec(s1, s2, m, n - 1);
}
bool isSubSeq(string& s1, string& s2){
int m = s1.length();
int n = s2.length();
if (m > n) return false;
return isSubSeqRec(s1, s2, m, n);
}
int main(){
string s1 = "gksrek";
string s2 = "geeksforgeeks";
if (isSubSeq(s1, s2))
cout << "true";
else
cout << "false";
return 0;
}
class GFG {
static boolean isSubSeqRec(String s1, String s2, int m, int n) {
// Base Cases
if (m == 0)
return true;
if (n == 0)
return false;
// If last characters match
if (s1.charAt(m - 1) == s2.charAt(n - 1))
return isSubSeqRec(s1, s2, m - 1, n - 1);
// If last characters don't match
return isSubSeqRec(s1, s2, m, n - 1);
}
static boolean isSubSeq(String s1, String s2) {
int m = s1.length();
int n = s2.length();
if (m > n)
return false;
return isSubSeqRec(s1, s2, m, n);
}
public static void main(String[] args) {
String s1 = "gksrek";
String s2 = "geeksforgeeks";
if (isSubSeq(s1, s2))
System.out.println("true");
else
System.out.println("false");
}
}
def isSubSeqRec(s1, s2, m, n):
# Base Cases
if m == 0:
return True
if n == 0:
return False
# If last characters match
if s1[m - 1] == s2[n - 1]:
return isSubSeqRec(s1, s2, m - 1, n - 1)
# If last characters don't match
return isSubSeqRec(s1, s2, m, n - 1)
def isSubSeq(s1, s2):
m = len(s1)
n = len(s2)
if m > n:
return False
return isSubSeqRec(s1, s2, m, n)
if __name__ == "__main__":
s1 = "gksrek"
s2 = "geeksforgeeks"
if isSubSeq(s1, s2):
print("true")
else:
print("false")
using System;
class GFG {
static bool isSubSeqRec(string s1, string s2, int m, int n) {
// Base Cases
if (m == 0)
return true;
if (n == 0)
return false;
// If last characters match
if (s1[m - 1] == s2[n - 1])
return isSubSeqRec(s1, s2, m - 1, n - 1);
// If last characters don't match
return isSubSeqRec(s1, s2, m, n - 1);
}
static bool isSubSeq(string s1, string s2) {
int m = s1.Length;
int n = s2.Length;
if (m > n)
return false;
return isSubSeqRec(s1, s2, m, n);
}
static void Main() {
string s1 = "gksrek";
string s2 = "geeksforgeeks";
if (isSubSeq(s1, s2))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
function isSubSeqRec(s1, s2, m, n) {
// Base Cases
if (m === 0)
return true;
if (n === 0)
return false;
// If last characters match
if (s1[m - 1] === s2[n - 1])
return isSubSeqRec(s1, s2, m - 1, n - 1);
// If last characters don't match
return isSubSeqRec(s1, s2, m, n - 1);
}
function isSubSeq(s1, s2) {
let m = s1.length;
let n = s2.length;
if (m > n)
return false;
return isSubSeqRec(s1, s2, m, n);
}
// Driver code
function main() {
let s1 = "gksrek";
let s2 = "geeksforgeeks";
if (isSubSeq(s1, s2))
console.log("true");
else
console.log("false");
}
main();
Output
true
Iterative - O(n)Time and O(1) Space
The idea is to use two pointers, one pointer will start from start of s1 and another will start from start of s2. If current characters on both the indexes are same then increment both pointers otherwise increment the pointer which is pointing s2.
Follow the steps below to solve the problem:
- Initialize the pointers i and j with zero, where i is the pointer to s1 and j is the pointer to s2.
- If s1[i] = s2[j] then increment both i and j by 1.
- Otherwise, increment only j by 1.
- If i reaches the end of s1 then return true else return false.
Below is the implementation of the above approach
#include <iostream>
using namespace std;
bool isSubSeq(string& s1, string& s2){
int m = s1.length(), n = s2.length();
// For s1 to be subsequence, its length must
// smaller than s2
if (m > n) return false;
int i = 0, j = 0;
while (i < m && j < n) {
if (s1[i] == s2[j])
i++;
j++;
}
return i == m;
}
int main(){
string s1 = "gksrek";
string s2 = "geeksforgeeks";
isSubSeq(s1, s2) ? cout << "true" : cout << "false";
return 0;
}
class GFG {
static boolean isSubSeq(String s1, String s2) {
int m = s1.length(), n = s2.length();
// For s1 to be a subsequence, its length must be smaller than s2
if (m > n)
return false;
int i = 0, j = 0;
while (i < m && j < n) {
if (s1.charAt(i) == s2.charAt(j))
i++;
j++;
}
return i == m;
}
public static void main(String[] args) {
String s1 = "gksrek";
String s2 = "geeksforgeeks";
if (isSubSeq(s1, s2))
System.out.println("true");
else
System.out.println("false");
}
}
def isSubSeq(s1, s2):
m, n = len(s1), len(s2)
# For s1 to be a subsequence, its length must be smaller than s2
if m > n:
return False
i, j = 0, 0
while i < m and j < n:
if s1[i] == s2[j]:
i += 1
j += 1
return i == m
if __name__ == "__main__":
s1 = "gksrek"
s2 = "geeksforgeeks"
if isSubSeq(s1, s2):
print("true")
else:
print("false")
using System;
class GFG {
static bool isSubSeq(string s1, string s2) {
int m = s1.Length, n = s2.Length;
// For s1 to be a subsequence, its length must be smaller than s2
if (m > n)
return false;
int i = 0, j = 0;
while (i < m && j < n) {
if (s1[i] == s2[j])
i++;
j++;
}
return i == m;
}
static void Main() {
string s1 = "gksrek";
string s2 = "geeksforgeeks";
if (isSubSeq(s1, s2))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
function isSubSeq(s1, s2) {
let m = s1.length, n = s2.length;
// For s1 to be a subsequence, its length must be smaller than s2
if (m > n)
return false;
let i = 0, j = 0;
while (i < m && j < n) {
if (s1[i] === s2[j])
i++;
j++;
}
return i === m;
}
// Driver code
function main() {
let s1 = "gksrek";
let s2 = "geeksforgeeks";
if (isSubSeq(s1, s2))
console.log("true");
else
console.log("false");
}
main();
Output
true
Â