Check if a string can be obtained by rotating another string 2 places | Method to check if string2 is obtained by string 1 ; Initialize string as anti - clockwise rotation ; Initialize string as clock wise rotation ; Check if any of them is equal to string1 ; Driver code
function isRotated ( str1 , str2 ) { if ( str1 . length != str2 . length ) return false ; if ( str1 . length < 2 ) { return str1 . localeCompare ( str2 ) ; } let clock_rot = " " ; let anticlock_rot = " " ; let len = str2 . length ; anticlock_rot = anticlock_rot + str2 . substring ( len - 2 , len + 1 ) + str2 . substring ( 0 , len - 1 ) ; clock_rot = clock_rot + str2 . substring ( 2 , str2 . length - 2 + 1 ) + str2 . substring ( 0 , 2 + 1 ) ; return ( str1 . localeCompare ( clock_rot ) || str1 . localeCompare ( anticlock_rot ) ) ; } let str1 = " " ; let str2 = " " ; document . write ( isRotated ( str1 , str2 ) ? " " : " " ) ;