Check 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
def leastLexiString ( s ) : NEW_LINE INDENT if ( len ( s ) & 1 != 0 ) : NEW_LINE INDENT return s NEW_LINE DEDENT x = leastLexiString ( s [ 0 : int ( len ( s ) / 2 ) ] ) NEW_LINE y = leastLexiString ( s [ int ( len ( s ) / 2 ) : len ( s ) ] ) NEW_LINE return min ( x + y , y + x ) NEW_LINE DEDENT def areEquivalent ( a , b ) : NEW_LINE INDENT return ( leastLexiString ( a ) == leastLexiString ( b ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = " aaba " NEW_LINE b = " abaa " NEW_LINE if ( areEquivalent ( a , b ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT a = " aabb " NEW_LINE b = " abab " NEW_LINE if ( areEquivalent ( a , b ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT