Lexicographically minimum string rotation | Set 1 | This functionr return lexicographically minimum rotation of str ; Find length of given String ; Create an array of strings to store all rotations ; Create a concatenation of String with itself ; One by one store all rotations of str in array . A rotation is obtained by getting a substring of concat ; Sort all rotations ; Return the first rotation from the sorted array ; Driver code
function minLexRotation ( str ) { let n = str . length ; let arr = new Array ( n ) ; let concat = str + str ; for ( let i = 0 ; i < n ; i ++ ) { arr [ i ] = concat . substring ( i , i + n ) ; } arr . sort ( ) ; return arr [ 0 ] ; } document . write ( minLexRotation ( " " ) + " " ) ; document . write ( minLexRotation ( " " ) + " " ) ; document . write ( minLexRotation ( " " ) + " " ) ;