Check whether two strings are equivalent or not according to given condition | CPP Program to find whether two strings are equivalent or not according to given condition ; This function returns the least lexicogr aphical string obtained from its two halves ; Base Case - If string size is 1 ; Divide the string into its two halves ; Form least lexicographical string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string leastLexiString ( string s ) { if ( s . size ( ) & 1 ) return s ; string x = leastLexiString ( s . substr ( 0 , s . size ( ) / 2 ) ) ; string y = leastLexiString ( s . substr ( s . size ( ) / 2 ) ) ; return min ( x + y , y + x ) ; } bool areEquivalent ( string a , string b ) { return ( leastLexiString ( a ) == leastLexiString ( b ) ) ; } int main ( ) { string a = " aaba " ; string b = " abaa " ; if ( areEquivalent ( a , b ) ) cout << " YES " << endl ; else cout << " NO " << endl ; a = " aabb " ; b = " abab " ; if ( areEquivalent ( a , b ) ) cout << " YES " << endl ; else cout << " NO " << endl ; return 0 ; }