Write code to find lexicographic minimum in a circular array, e.g. for the array BCABDADAB, the lexicographic minimum is ABBCABDAD.
Source: Google Written Test
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’
- Concatenate ‘str’ with itself and store in a temporary string say ‘concat’.
- Create an array of strings to store all rotations of ‘str’. Let the array be ‘arr’.
- Find all rotations of ‘str’ by taking substrings of ‘concat’ at index 0, 1, 2..n-1. Store these rotations in arr[]
- Sort arr[] and return arr[0].
Following is the implementation of above solution.
C++
#include <iostream>
#include <algorithm>
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;
}
|
Java
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"));
}
}
|
Python3
def minLexRotation(str_) :
n = len(str_)
arr = [0] * n
concat = str_ + str_
for i in range(n) :
arr[i] = concat[i : n + i]
arr.sort()
return arr[0]
print(minLexRotation("GEEKSFORGEEKS"))
print(minLexRotation("GEEKSQUIZ"))
print(minLexRotation("BCABDADAB"))
|
C#
using System;
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, n);
}
Array.Sort(arr);
return arr[0];
}
public static void Main(String[] args)
{
Console.WriteLine(minLexRotation("GEEKSFORGEEKS"));
Console.WriteLine(minLexRotation("GEEKSQUIZ"));
Console.WriteLine(minLexRotation("BCABDADAB"));
}
}
|
Javascript
<script>
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("GEEKSFORGEEKS") + "</br>");
document.write(minLexRotation("GEEKSQUIZ") + "</br>");
document.write(minLexRotation("BCABDADAB") + "</br>");
</script>
|
OutputEEKSFORGEEKSG
EEKSQUIZG
ABBCABDAD
Lexicographically smallest rotated sequence | Set 2
Time complexity of the above solution is O(n2Logn) under the assumption that we have used a O(nLogn) sorting algorithm.
Auxiliary Space: O(n)
This problem can be solved using more efficient methods like Booth’s Algorithm which solves the problem in O(n) time. We will soon be covering these methods as separate posts.