Lexicographically minimum string rotation | Set 1
Last Updated :
07 Jul, 2022
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++
// A simple C++ program to find lexicographically minimum rotation
// of a given string
#include <iostream>
#include <algorithm>
using namespace std;
// This functionr return lexicographically minimum
// rotation of str
string 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 function
int 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 String
import 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 str
def 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 Code
print(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 String
using 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
JavaScript
<script>
// A simple Javascript program to find
// lexicographically minimum rotation
// of a given String
// This functionr return lexicographically
// minimum rotation of str
function minLexRotation(str)
{
// Find length of given String
let n = str.length;
// Create an array of strings
// to store all rotations
let arr = new Array(n);
// Create a concatenation of
// String with itself
let concat = str + str;
// One by one store all rotations
// of str in array. A rotation is
// obtained by getting a substring of concat
for(let i = 0; i < n; i++)
{
arr[i] = concat.substring(i, i + n);
}
// Sort all rotations
arr.sort();
// Return the first rotation
// from the sorted array
return arr[0];
}
// Driver code
document.write(minLexRotation("GEEKSFORGEEKS") + "</br>");
document.write(minLexRotation("GEEKSQUIZ") + "</br>");
document.write(minLexRotation("BCABDADAB") + "</br>");
// This code is contributed by divyeshrabadiya07
</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.
Similar Reads
Lexicographically minimum String possible Given three strings, S1, S2, and S3. The characters at the same index in S1 and S2 are considered equivalent and follow the transitive property, the task is to find and print the lexicographically smallest string that you can obtain from S3 by changing its characters to some other equivalent charact
8 min read
Javascript Program to Find 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.Source: Google Written TestExamples: Input: GEEKSQUIZOutput: EEKSQUIZGInput: GFGOutput: FGGInput: GEEKSFORGEEKSOutput: EEKSFORGEEKSGThe following is a simple solution. L
2 min read
Lexicographically smallest string after M operations Given a string S and integer M. The task is to perform exactly M operations to get lexicographical smallest string. In each operation, select one character optimally from the string and update it with immediate next character ( aaa -> aab ), so that string remain lexicographical smallest.Multiple
7 min read
Create lexicographically minimum String using given operation Given a string s of length N consisting of digits from 0 to 9. Find the lexicographically minimum sequence that can be obtained from given string s by performing the given operations: Selecting any position i in the string and delete the digit 'd' at ith position, Insert min(d+1, 9) on any position
7 min read
Find Lexicographically smallest String Given a circular string s, find whether there exists a permutation of s which is a K-periodic circular string and if it exists then find the lexicographically smallest permutation of s which is a K-periodic circular string. Return an empty string if there does not exist a valid permutation of s whic
6 min read