Lexicographically minimum string rotation | Set 1 | A simple C ++ 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 program to test above function
#include <iostream> NEW_LINE #include <algorithm> NEW_LINE using namespace std ; string minLexRotation ( string str ) { int n = str . length ( ) ; string arr [ n ] ; string concat = str + str ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = concat . substr ( i , n ) ; sort ( arr , arr + n ) ; return arr [ 0 ] ; } int main ( ) { cout << minLexRotation ( " GEEKSFORGEEKS " ) << endl ; cout << minLexRotation ( " GEEKSQUIZ " ) << endl ; cout << minLexRotation ( " BCABDADAB " ) << endl ; }