Check whether two strings contain same characters in same order | C ++ implementation of the approach ; string class has a constructor that allows us to specify size of string as first parameter and character to be filled in given size as second parameter . ; Function that returns true if the given strings contain same characters in same order ; Get the first character of both strings ; Now if there are adjacent similar character remove that character from s1 ; Now if there are adjacent similar character remove that character from s2 ; If both the strings are equal then return true ; Driver code
#include <iostream> NEW_LINE 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 ; }