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
< ? php function leastLexiString ( $ s ) { if ( strlen ( $ s ) & 1 ) return $ s ; $ x = leastLexiString ( substr ( $ s , 0 , floor ( strlen ( $ s ) / 2 ) ) ) ; $ y = leastLexiString ( substr ( $ s , floor ( strlen ( $ s ) / 2 ) , strlen ( $ s ) ) ) ; return min ( $ x . $ y , $ y . $ x ) ; } function areEquivalent ( $ a , $ b ) { return ( leastLexiString ( $ a ) == leastLexiString ( $ b ) ) ; } $ a = " aaba " ; $ b = " abaa " ; if ( areEquivalent ( $ a , $ b ) ) echo " YES " , " STRNEWLINE " ; else echo " NO " , " STRNEWLINE " ; $ a = " aabb " ; $ b = " abab " ; if ( areEquivalent ( $ a , $ b ) ) echo " YES " , " STRNEWLINE " ; else echo " NO " , " STRNEWLINE " ; ? >