Lexicographically minimum string rotation | Set 1 | A simple Java program to find lexicographically minimum rotation of a given String ; 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
import java . util . * ; class GFG { static String minLexRotation ( String str ) { int n = str . length ( ) ; String arr [ ] = new String [ n ] ; String concat = str + str ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = concat . substring ( i , i + n ) ; } Arrays . sort ( arr ) ; return arr [ 0 ] ; } public static void main ( String [ ] args ) { System . out . println ( minLexRotation ( " GEEKSFORGEEKS " ) ) ; System . out . println ( minLexRotation ( " GEEKSQUIZ " ) ) ; System . out . println ( minLexRotation ( " BCABDADAB " ) ) ; } }