GeeksforGeeks App
Open App
Browser
Continue

# Lexicographically minimum string rotation | Set 1

Write code to find lexicographic minimum in a circular array, e.g. for the array BCABDADAB, the lexicographic minimum is ABBCABDAD.

More Examples:

Input:  GEEKSQUIZ
Output: EEKSQUIZG

Input:  GFG
Output: FGG

Input:  GEEKSFORGEEKS
Output: EEKSFORGEEKSG

Following is a simple solution. Let the given string be ‘str’

1. Concatenate ‘str’ with itself and store in a temporary string say ‘concat’.
2. Create an array of strings to store all rotations of ‘str’. Let the array be ‘arr’.
3. Find all rotations of ‘str’ by taking substrings of ‘concat’ at index 0, 1, 2..n-1. Store these rotations in arr[]
4.  Sort arr[] and return arr[0].

Following is the implementation of above solution.

## C++

 // A simple C++ program to find lexicographically minimum rotation// of a given string#include #include using namespace std; // This functionr return lexicographically minimum// rotation of strstring minLexRotation(string str){    // Find length of given string    int n = str.length();     // Create an array of strings to store all rotations    string arr[n];     // Create a concatenation of string with itself    string concat = str + str;     // One by one store all rotations of str in array.    // A rotation is obtained by getting a substring of concat    for (int i = 0; i < n; i++)        arr[i] = concat.substr(i, n);     // Sort all rotations    sort(arr, arr+n);     // Return the first rotation from the sorted array    return arr[0];} // Driver program to test above functionint main(){    cout << minLexRotation("GEEKSFORGEEKS") << endl;    cout << minLexRotation("GEEKSQUIZ") << endl;    cout << minLexRotation("BCABDADAB") << endl;}

## Java

 // A simple Java program to find// lexicographically minimum rotation// of a given Stringimport java.util.*; class GFG{     // This functionr return lexicographically    // minimum rotation of str    static String minLexRotation(String str)    {        // Find length of given String        int n = str.length();         // Create an array of strings        // to store all rotations        String arr[] = new String[n];         // Create a concatenation of        // String with itself        String concat = str + str;         // One by one store all rotations        // of str in array. A rotation is        // obtained by getting a substring of concat        for (int i = 0; i < n; i++)        {            arr[i] = concat.substring(i, i + n);        }         // Sort all rotations        Arrays.sort(arr);         // Return the first rotation        // from the sorted array        return arr[0];    }     // Driver code    public static void main(String[] args)    {        System.out.println(minLexRotation("GEEKSFORGEEKS"));        System.out.println(minLexRotation("GEEKSQUIZ"));        System.out.println(minLexRotation("BCABDADAB"));    }} // This code is contributed by 29AjayKumar

## Python3

 # A simple Python3 program to find lexicographically# minimum rotation of a given string # This function return lexicographically minimum# rotation of strdef minLexRotation(str_) :     # Find length of given string    n = len(str_)     # Create an array of strings to store all rotations    arr = [0] * n     # Create a concatenation of string with itself    concat = str_ + str_     # One by one store all rotations of str in array.    # A rotation is obtained by getting a substring of concat    for i in range(n) :        arr[i] = concat[i : n + i]     # Sort all rotations    arr.sort()     # Return the first rotation from the sorted array    return arr[0] # Driver Codeprint(minLexRotation("GEEKSFORGEEKS"))print(minLexRotation("GEEKSQUIZ"))print(minLexRotation("BCABDADAB")) # This code is contributed by divyamohan123

## C#

 // A simple C# program to find// lexicographically minimum rotation// of a given Stringusing System; class GFG{     // This functionr return lexicographically    // minimum rotation of str    static String minLexRotation(String str)    {        // Find length of given String        int n = str.Length;         // Create an array of strings        // to store all rotations        String []arr = new String[n];         // Create a concatenation of        // String with itself        String concat = str + str;         // One by one store all rotations        // of str in array. A rotation is        // obtained by getting a substring of concat        for (int i = 0; i < n; i++)        {            arr[i] = concat.Substring(i, n);        }         // Sort all rotations        Array.Sort(arr);         // Return the first rotation        // from the sorted array        return arr[0];    }     // Driver code    public static void Main(String[] args)    {        Console.WriteLine(minLexRotation("GEEKSFORGEEKS"));        Console.WriteLine(minLexRotation("GEEKSQUIZ"));        Console.WriteLine(minLexRotation("BCABDADAB"));    }} // This code is contributed by Rajput-Ji



Output

EEKSFORGEEKSG
EEKSQUIZG