Given two strings s1 and s2, the task is to find whether the two strings contain the same characters that occur in the same order. For example string “Geeks” and string “Geks” contain the same characters in same order.
Examples:
Input: s1 = “Geeks”, s2 = “Geks”
Output: Yes
Input: s1 = “Arnab”, s2 = “Andrew”
Output: No
Approach: We have two strings now we have to check whether the strings contain the same characters in the same order. So we will replace the contiguous similar element with a single element i.e. if we have “eee”, we will replace it with a single “e”. Now we will check that both the strings are equal or not. If equal then print Yes else No.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
string getString( char x)
{
string s(1, x);
return s;
}
bool solve(string s1, string s2)
{
string a = getString(s1[0]), b = getString(s2[0]);
for ( int i = 1; i < s1.length(); i++)
if (s1[i] != s1[i - 1]) {
a += getString(s1[i]);
}
for ( int i = 1; i < s2.length(); i++)
if (s2[i] != s2[i - 1]) {
b += getString(s2[i]);
}
if (a == b)
return true ;
return false ;
}
int main()
{
string s1 = "Geeks" , s2 = "Geks" ;
if (solve(s1, s2))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class temp
{
static String getString( char x)
{
String s = String.valueOf(x);
return s;
}
static boolean solve(String s1, String s2)
{
String a = getString(s1.charAt( 0 )),
b = getString(s2.charAt( 0 ));
for ( int i = 1 ; i < s1.length(); i++)
if (s1.charAt(i) != s1.charAt(i - 1 ))
{
a += getString(s1.charAt(i));
}
for ( int i = 1 ; i < s2.length(); i++)
if (s2.charAt(i) != s2.charAt(i - 1 ))
{
b += getString(s2.charAt(i));
}
if (a.equals(b))
return true ;
return false ;
}
public static void main(String[] args)
{
String s1 = "Geeks" , s2 = "Geks" ;
if (solve(s1, s2))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def getString(x):
return x
def solve(s1, s2):
a = getString(s1[ 0 ])
b = getString(s2[ 0 ])
for i in range ( 1 , len (s1)):
if s1[i] ! = s1[i - 1 ]:
a + = getString(s1[i])
for i in range ( 1 , len (s2)):
if s2[i] ! = s2[i - 1 ]:
b + = getString(s2[i])
if a = = b:
return True
return False
s1 = "Geeks"
s2 = "Geks"
if solve(s1, s2):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class temp
{
static String getString( char x)
{
String s = String.Join( "" ,x);
return s;
}
static Boolean solve(String s1, String s2)
{
String a = getString(s1[0]),
b = getString(s2[0]);
for ( int i = 1; i < s1.Length; i++)
if (s1[i] != s1[i - 1]) {
a += getString(s1[i]);
}
for ( int i = 1; i < s2.Length; i++)
if (s2[i] != s2[i - 1]) {
b += getString(s2[i]);
}
if (a == b)
return true ;
return false ;
}
public static void Main(String[] args)
{
String s1 = "Geeks" , s2 = "Geks" ;
if (solve(s1, s2))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function getString(x)
{
return x
}
function solve(s1, s2)
{
var a = getString(s1[0]), b = getString(s2[0]);
for ( var i = 1; i < s1.length; i++)
if (s1[i] != s1[i - 1]) {
a += getString(s1[i]);
}
for ( var i = 1; i < s2.length; i++)
if (s2[i] != s2[i - 1]) {
b += getString(s2[i]);
}
if (a == b)
return true ;
return false ;
}
var s1 = "Geeks" , s2 = "Geks" ;
if (solve(s1, s2))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(m + n)
Auxiliary Space: O(m + n), where m and n are the length of the given strings s1 and s2 respectively.
Using Recursion
C++
#include <iostream>
using namespace std;
bool checkSequence(string a, string b)
{
if (b.size() == 0)
return true ;
if (a.size() == 0)
return false ;
if (a[0] == b[0])
return checkSequence(a.substr(1), b.substr(1));
else
return checkSequence(a.substr(1), b);
}
int main()
{
string s1 = "Geeks" , s2 = "Geks" ;
if (checkSequence(s1, s2))
cout << "Yes" ;
else
cout << "No" ;
}
|
Java
import java.io.*;
class GFG {
public static boolean checkSequence(String a, String b) {
if (b.length()== 0 )
return true ;
if (a.length() == 0 )
return false ;
if (a.charAt( 0 ) == b.charAt( 0 ))
return checkSequence(a.substring( 1 ), b.substring( 1 ));
else
return checkSequence(a.substring( 1 ), b);
}
public static void main(String[] args)
{
String s1 = "Geeks" , s2 = "Geks" ;
if (checkSequence(s1, s2))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python
def checkSequence(a, b):
if len (b) = = 0 :
return True
if len (a) = = 0 :
return False
if (a[ 0 ] = = b[ 0 ]):
return checkSequence(a[ 1 :], b[ 1 :])
else :
return checkSequence(a[ 1 :], b)
if __name__ = = '__main__' :
s1 = "Geeks"
s2 = "Geks"
if (checkSequence(s1, s2)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class temp
{
public static bool checkSequence(String a, String b)
{
if (b.Length == 0)
return true ;
if (a.Length == 0)
return false ;
if (a[0] == b[0])
return checkSequence(a.Substring(1), b.Substring(1));
else
return checkSequence(a.Substring(1), b);
}
public static void Main(String[] args)
{
String s1 = "Geeks" , s2 = "Geks" ;
if (checkSequence(s1, s2))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function checkSequence(a, b)
{
if (b.length == 0)
return true ;
if (a.length == 0)
return false ;
if (a[0] == b[0])
return checkSequence(a.substring(1),
b.substring(1));
else
return checkSequence(a.substring(1), b);
}
let s1 = "Geeks" , s2 = "Geks" ;
if (checkSequence(s1, s2))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time complexity: O(max(M, N)) for strings of length M and N respectively.
Auxiliary Space: O(max(M, N)), due to recursive call stacks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 Dec, 2022
Like Article
Save Article