Lexicographically smallest rotated sequence | Set 2
Write code to find lexicographic minimum in a circular array, e.g. for the array BCABDADAB, the lexicographic minimum is ABBCABDAD
Input Constraint: 1 < n < 1000
Examples:
Input: GEEKSQUIZ
Output: EEKSQUIZG
Input: GFG
Output: FGG
Input : CAPABCQ
Output : ABCQCAP
We have discussed a O(n2Logn) solution in Lexicographically minimum string rotation | Set 1. Here we need to find the starting index of minimum rotation and then print the rotation.
1) Initially assume 0 to be current min
starting index.
2) Loop through i = 1 to n-1.
a) For each i compare sequence starting
at i with current min starting index
b) If sequence starting at i is lexicographically
smaller, update current min starting
index.
Here is pseudo-code for algorithm
function findIndexForSmallestSequence(S, n):
result = 0
for i = 1:n-1
if (sequence beginning at i <
sequence beginning at result)
result = i
end if
end for
return result
Here is implementation of above algorithm.
C++
#include <iostream>
using namespace std;
bool compareSeq( char S[], int x, int y, int n)
{
for ( int i = 0; i < n; i++) {
if (S[x] < S[y])
return false ;
else if (S[x] > S[y])
return true ;
x = (x + 1) % n;
y = (y + 1) % n;
}
return true ;
}
int smallestSequence( char S[], int n)
{
int index = 0;
for ( int i = 1; i < n; i++)
if (compareSeq(S, index, i, n))
index = i;
return index;
}
void printSmallestSequence( char S[], int n)
{
int starting_index = smallestSequence(S, n);
for ( int i = 0; i < n; i++)
cout << S[(starting_index + i) % n];
}
int main()
{
char S[] = "DCACBCAA" ;
int n = 8;
printSmallestSequence(S, n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class LexoSmallest {
static boolean compareSeq( char [] S, int x, int y, int n)
{
for ( int i = 0 ; i < n; i++) {
if (S[x] < S[y])
return false ;
else if (S[x] > S[y])
return true ;
x = (x + 1 ) % n;
y = (y + 1 ) % n;
}
return true ;
}
static int smallestSequence( char [] S, int n)
{
int index = 0 ;
for ( int i = 1 ; i < n; i++)
if (compareSeq(S, index, i, n))
index = i;
return index;
}
static void printSmallestSequence(String str, int n)
{
char [] S = str.toCharArray();
int starting_index = smallestSequence(S, n);
for ( int i = 0 ; i < n; i++)
System.out.print(S[(starting_index + i) % n]);
}
public static void main(String[] args)
{
String S = "DCACBCAA" ;
int n = 8 ;
printSmallestSequence(S, n);
}
}
|
Python 3
import copy
def printSmallestSequence(s):
m = copy.copy(s)
for i in range ( len (s) - 1 ):
if m > s[i:] + s[:i]:
m = s[i:] + s[:i]
return m
if __name__ = = '__main__' :
st = 'DCACBCAA'
print (printSmallestSequence(st))
|
C#
using System;
class LexoSmallest {
static bool compareSeq( string S, int x, int y, int n)
{
for ( int i = 0; i < n; i++) {
if (S[x] < S[y])
return false ;
else if (S[x] > S[y])
return true ;
x = (x + 1) % n;
y = (y + 1) % n;
}
return true ;
}
static int smallestSequence( string S, int n)
{
int index = 0;
for ( int i = 1; i < n; i++)
if (compareSeq(S, index, i, n))
index = i;
return index;
}
static void printSmallestSequence( string str, int n)
{
int starting_index = smallestSequence(str, n);
for ( int i = 0; i < n; i++)
Console.Write(str[(starting_index + i) % n]);
}
public static void Main()
{
string S = "DCACBCAA" ;
int n = 8;
printSmallestSequence(S, n);
}
}
|
PHP
<?php
function compareSeq( $S , $x , $y , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
if ( $S [ $x ] < $S [ $y ])
return false;
else if ( $S [ $x ] > $S [ $y ])
return true;
$x = ( $x + 1) % $n ;
$y = ( $y + 1) % $n ;
}
return true;
}
function smallestSequence( $S , $n )
{
$index = 0;
for ( $i = 1; $i < $n ; $i ++)
if (compareSeq( $S , $index , $i , $n ))
$index = $i ;
return $index ;
}
function printSmallestSequence( $S , $n )
{
$starting_index = smallestSequence( $S , $n );
for ( $i = 0; $i < $n ; $i ++)
echo $S [( $starting_index + $i ) % $n ];
}
$S = "DCACBCAA" ;
$n = 8;
printSmallestSequence( $S , $n );
?>
|
Javascript
<script>
function compareSeq(S,x,y,n)
{
for (let i = 0; i < n; i++)
{
if (S[x] < S[y])
return false ;
else if (S[x] > S[y])
return true ;
x = (x + 1) % n;
y = (y + 1) % n;
}
return true ;
}
function smallestSequence(S,n)
{
let index = 0;
for (let i = 1; i < n; i++)
if (compareSeq(S, index, i, n))
index = i;
return index;
}
function printSmallestSequence(str,n)
{
let S = str.split( "" );
let starting_index = smallestSequence(S, n);
for (let i = 0; i < n; i++)
document.write(S[(starting_index + i) % n]);
}
let S = "DCACBCAA" ;
let n = 8;
printSmallestSequence(S, n);
</script>
|
Time Complexity : O(n^2)
Auxiliary Space : O(1)
Last Updated :
13 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...