Check whether the string S1 can be made equal to S2 with the given operation | CPP implementation of the approach ; Function to return the string formed by the odd indexed characters of s ; Function to return the string formed by the even indexed characters of s ; Function that returns true if s1 can be made equal to s2 with the given operation ; Get the string formed by the even indexed characters of s1 ; Get the string formed by the even indexed characters of s2 ; Get the string formed by the odd indexed characters of s1 ; Get the string formed by the odd indexed characters of s2 ; Sorting all the lists ; If the strings can be made equal ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string partOdd ( string s ) { string st = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( i % 2 != 0 ) st += s [ i ] ; } return st ; } string partEven ( string str ) { string s = " " ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( i % 2 == 0 ) s += str [ i ] ; } return s ; } bool canBeMadeEqual ( string s1 , string s2 ) { string even_s1 = partEven ( s1 ) ; string even_s2 = partEven ( s2 ) ; string odd_s1 = partOdd ( s1 ) ; string odd_s2 = partOdd ( s2 ) ; sort ( even_s1 . begin ( ) , even_s1 . end ( ) ) ; sort ( even_s2 . begin ( ) , even_s2 . end ( ) ) ; sort ( odd_s1 . begin ( ) , odd_s1 . end ( ) ) ; sort ( odd_s2 . begin ( ) , odd_s2 . end ( ) ) ; if ( even_s1 == even_s2 and odd_s1 == odd_s2 ) return true ; return false ; } int main ( ) { string s1 = " cdab " ; string s2 = " abcd " ; if ( canBeMadeEqual ( s1 , s2 ) ) cout << " Yes " << endl ; else cout << " No " << endl ; }