Posted on by Kalkicode
Code String

Lexicographically minimum string rotation

Here given code implementation process.

``````import java.util.Arrays;
/*
Java program for
Lexicographically minimum string rotation
*/
public class Lexicographically
{

public void minimumString(String text)
{
int n = text.length();

if(n <= 0)
{
return;
}

String auxiliary = text + text;

// Use to collect possible rotation
String []record = new String[n];

// Collect the possible rotation
for (int i = 0; i < n ; ++i )
{
record[i] = auxiliary.substring(i,i+n);
}

// Sort record element
Arrays.sort(record);
// Display given string
System.out.println(" Given  : "+text);
// Display result
System.out.println(" Result : "+record[0]);

}
public static void main(String[] args)
{
// Test
/*
ATMAMOUNT
TMAMOUNTA
MAMOUNTAT
AMOUNTATM
MOUNTATMA
OUNTATMAM
UNTATMAMO
NTATMAMOU
TATMAMOUN
----------
AMOUNTATM  <- Result
*/
/*
LANGUAGE
ANGUAGEL
NGUAGELA
GUAGELAN
UAGELANG
AGELANGU
GELANGUA
ELANGUAG
--------
AGELANGU   <- Result
*/
/*
EBILL
BILLE
ILLEB
LLEBI
LEBIL
--------
BILLE   <- Result
*/

}
}``````

Output

`````` Given  : ATMAMOUNT
Result : AMOUNTATM
Given  : LANGUAGE
Result : AGELANGU
Given  : EBILL
Result : BILLE``````
``````// Include header file
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;
/*
C++ program for
Lexicographically minimum string rotation
*/
class Lexicographically
{
public: void minimumString(string text)
{
int n = text.length();
if (n <= 0)
{
return;
}
string auxiliary = text  +  text;
// Use to collect possible rotation
string record[n];
// Collect the possible rotation
for (int i = 0; i < n; ++i)
{
record[i] = auxiliary.substr(i, n);
}
// Sort record element
sort(record, record + n);
// Display given string
cout << " Given  : " << text << endl;
// Display result
cout << " Result : " << record[0] << endl;
}
};
int main()
{
// Test
/*
ATMAMOUNT
TMAMOUNTA
MAMOUNTAT
AMOUNTATM
MOUNTATMA
OUNTATMAM
UNTATMAMO
NTATMAMOU
TATMAMOUN
----------
AMOUNTATM  <- Result

*/
/*
LANGUAGE
ANGUAGEL
NGUAGELA
GUAGELAN
UAGELANG
AGELANGU
GELANGUA
ELANGUAG
--------
AGELANGU   <- Result

*/
/*
EBILL
BILLE
ILLEB
LLEBI
LEBIL
--------
BILLE   <- Result

*/
return 0;
}``````

Output

`````` Given  : ATMAMOUNT
Result : AMOUNTATM
Given  : LANGUAGE
Result : AGELANGU
Given  : EBILL
Result : BILLE``````
``````// Include namespace system
using System;
/*
Csharp program for
Lexicographically minimum string rotation
*/
public class Lexicographically
{
public void minimumString(String text)
{
int n = text.Length;
if (n <= 0)
{
return;
}
String auxiliary = text + text;
// Use to collect possible rotation
String[] record = new String[n];
// Collect the possible rotation
for (int i = 0; i < n; ++i)
{
record[i] = auxiliary.Substring(i, n);
}
// Sort record element
Array.Sort(record);
// Display given string
Console.WriteLine(" Given  : " + text);
// Display result
Console.WriteLine(" Result : " + record[0]);
}
public static void Main(String[] args)
{
// Test
/*
ATMAMOUNT
TMAMOUNTA
MAMOUNTAT
AMOUNTATM
MOUNTATMA
OUNTATMAM
UNTATMAMO
NTATMAMOU
TATMAMOUN
----------
AMOUNTATM  <- Result

*/
/*
LANGUAGE
ANGUAGEL
NGUAGELA
GUAGELAN
UAGELANG
AGELANGU
GELANGUA
ELANGUAG
--------
AGELANGU   <- Result

*/
/*
EBILL
BILLE
ILLEB
LLEBI
LEBIL
--------
BILLE   <- Result

*/
}
}``````

Output

`````` Given  : ATMAMOUNT
Result : AMOUNTATM
Given  : LANGUAGE
Result : AGELANGU
Given  : EBILL
Result : BILLE``````
``````package main

import "sort"
import "fmt"
/*
Go program for
Lexicographically minimum string rotation
*/

func minimumString(text string) {
var n int = len(text)
if n <= 0 {
return
}
var auxiliary string = text + text
// Use to collect possible rotation
var record = make([]string,n)
var runes = []rune(auxiliary)
var temp = ""
// Collect the possible rotation
for i := 0 ; i < n ; i++ {
temp =  string(runes[i:i+n])
record[i] = temp
}
sort.Strings(record)
// Display given string
fmt.Println(" Given  : ", text)
// Display result
fmt.Println(" Result : ", record[0])
}
func main() {

// Test
/*
ATMAMOUNT
TMAMOUNTA
MAMOUNTAT
AMOUNTATM
MOUNTATMA
OUNTATMAM
UNTATMAMO
NTATMAMOU
TATMAMOUN
----------
AMOUNTATM  <- Result

*/
minimumString("ATMAMOUNT")
/*
LANGUAGE
ANGUAGEL
NGUAGELA
GUAGELAN
UAGELANG
AGELANGU
GELANGUA
ELANGUAG
--------
AGELANGU   <- Result

*/
minimumString("LANGUAGE")
/*
EBILL
BILLE
ILLEB
LLEBI
LEBIL
--------
BILLE   <- Result

*/
minimumString("EBILL")
}``````

Output

`````` Given  : ATMAMOUNT
Result : AMOUNTATM
Given  : LANGUAGE
Result : AGELANGU
Given  : EBILL
Result : BILLE``````
``````<?php
/*
Php program for
Lexicographically minimum string rotation
*/
class Lexicographically
{
public	function minimumString(\$text)
{
\$n = strlen(\$text);
if (\$n <= 0)
{
return;
}
\$auxiliary = \$text.\$text;
// Use to collect possible rotation
\$record = array_fill(0, \$n, "");
// Collect the possible rotation
for (\$i = 0; \$i < \$n; ++\$i)
{
\$record[\$i] = substr(\$auxiliary, \$i, \$n);
}
// Sort record element
sort(\$record);
// Display given string
echo(" Given  : ".\$text."\n");
// Display result
echo(" Result : ".\$record[0]."\n");
}
}

function main()
{
// Test
/*
ATMAMOUNT
TMAMOUNTA
MAMOUNTAT
AMOUNTATM
MOUNTATMA
OUNTATMAM
UNTATMAMO
NTATMAMOU
TATMAMOUN
----------
AMOUNTATM  <- Result

*/
/*
LANGUAGE
ANGUAGEL
NGUAGELA
GUAGELAN
UAGELANG
AGELANGU
GELANGUA
ELANGUAG
--------
AGELANGU   <- Result

*/
/*
EBILL
BILLE
ILLEB
LLEBI
LEBIL
--------
BILLE   <- Result

*/
}
main();``````

Output

`````` Given  : ATMAMOUNT
Result : AMOUNTATM
Given  : LANGUAGE
Result : AGELANGU
Given  : EBILL
Result : BILLE``````
``````/*
Node JS program for
Lexicographically minimum string rotation
*/
class Lexicographically
{
minimumString(text)
{
var n = text.length;
if (n <= 0)
{
return;
}
var auxiliary = text + text;
// Use to collect possible rotation
var record = Array(n).fill("");
// Collect the possible rotation
for (var i = 0; i < n; ++i)
{
record[i] = auxiliary.substring(i, i+n);
}
// Sort record element
record.sort();
// Display given string
console.log(" Given  : " + text);
// Display result
console.log(" Result : " + record[0]);
}
}

function main()
{
// Test
/*
ATMAMOUNT
TMAMOUNTA
MAMOUNTAT
AMOUNTATM
MOUNTATMA
OUNTATMAM
UNTATMAMO
NTATMAMOU
TATMAMOUN
----------
AMOUNTATM  <- Result

*/
/*
LANGUAGE
ANGUAGEL
NGUAGELA
GUAGELAN
UAGELANG
AGELANGU
GELANGUA
ELANGUAG
--------
AGELANGU   <- Result

*/
/*
EBILL
BILLE
ILLEB
LLEBI
LEBIL
--------
BILLE   <- Result

*/
}
main();``````

Output

`````` Given  : ATMAMOUNT
Result : AMOUNTATM
Given  : LANGUAGE
Result : AGELANGU
Given  : EBILL
Result : BILLE``````
``````#    Python 3 program for
#    Lexicographically minimum string rotation
class Lexicographically :
def minimumString(self, text) :
n = len(text)
if (n <= 0) :
return

auxiliary = text + text
#  Use to collect possible rotation
record = [""] * (n)
i = 0
#  Collect the possible rotation
while (i < n) :
record[i] = auxiliary[i: i + n]
i += 1

#  Sort record element
record.sort()
#  Display given string
print(" Given  : ", text)
#  Display result
print(" Result : ", record[0])

def main() :
#  Test
#    ATMAMOUNT
#    TMAMOUNTA
#    MAMOUNTAT
#    AMOUNTATM
#    MOUNTATMA
#    OUNTATMAM
#    UNTATMAMO
#    NTATMAMOU
#    TATMAMOUN
#    ----------
#    AMOUNTATM  <- Result
#    LANGUAGE
#    ANGUAGEL
#    NGUAGELA
#    GUAGELAN
#    UAGELANG
#    AGELANGU
#    GELANGUA
#    ELANGUAG
#    --------
#    AGELANGU   <- Result
#    EBILL
#    BILLE
#    ILLEB
#    LLEBI
#    LEBIL
#    --------
#    BILLE   <- Result

if __name__ == "__main__": main()``````

Output

`````` Given  :  ATMAMOUNT
Result :  AMOUNTATM
Given  :  LANGUAGE
Result :  AGELANGU
Given  :  EBILL
Result :  BILLE``````
``````#    Ruby program for
#    Lexicographically minimum string rotation
class Lexicographically
def minimumString(text)
n = text.length
if (n <= 0)
return
end

auxiliary = text + text
#  Use to collect possible rotation
record = Array.new(n) {""}
i = 0
#  Collect the possible rotation
while (i < n)
record[i] = auxiliary[i...(i + n)]
i += 1
end

#  Sort record element
record = record.sort
#  Display given string
print(" Given  : ", text, "\n")
#  Display result
print(" Result : ", record[0], "\n")
end

end

def main()
#  Test
#    ATMAMOUNT
#    TMAMOUNTA
#    MAMOUNTAT
#    AMOUNTATM
#    MOUNTATMA
#    OUNTATMAM
#    UNTATMAMO
#    NTATMAMOU
#    TATMAMOUN
#    ----------
#    AMOUNTATM  <- Result
#    LANGUAGE
#    ANGUAGEL
#    NGUAGELA
#    GUAGELAN
#    UAGELANG
#    AGELANGU
#    GELANGUA
#    ELANGUAG
#    --------
#    AGELANGU   <- Result
#    EBILL
#    BILLE
#    ILLEB
#    LLEBI
#    LEBIL
#    --------
#    BILLE   <- Result
end

main()``````

Output

`````` Given  : ATMAMOUNT
Result : AMOUNTATM
Given  : LANGUAGE
Result : AGELANGU
Given  : EBILL
Result : BILLE
``````
``````import scala.collection.mutable._;
/*
Scala program for
Lexicographically minimum string rotation
*/
class Lexicographically()
{
def minimumString(text: String): Unit = {
var n: Int = text.length();
if (n <= 0)
{
return;
}
var auxiliary: String = text + text;
// Use to collect possible rotation
var record: Array[String] = Array.fill[String](n)("");
var i: Int = 0;
// Collect the possible rotation
while (i < n)
{
record(i) = auxiliary.substring(i, i + n);
i += 1;
}
// Sort record element
record = record.sorted;
// Display given string
println(" Given  : " + text);
// Display result
println(" Result : " + record(0));
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Lexicographically = new Lexicographically();
// Test
/*
ATMAMOUNT
TMAMOUNTA
MAMOUNTAT
AMOUNTATM
MOUNTATMA
OUNTATMAM
UNTATMAMO
NTATMAMOU
TATMAMOUN
----------
AMOUNTATM  <- Result

*/
/*
LANGUAGE
ANGUAGEL
NGUAGELA
GUAGELAN
UAGELANG
AGELANGU
GELANGUA
ELANGUAG
--------
AGELANGU   <- Result

*/
/*
EBILL
BILLE
ILLEB
LLEBI
LEBIL
--------
BILLE   <- Result

*/
}
}``````

Output

`````` Given  : ATMAMOUNT
Result : AMOUNTATM
Given  : LANGUAGE
Result : AGELANGU
Given  : EBILL
Result : BILLE``````
``````import Foundation;
/*
Swift 4 program for
Lexicographically minimum string rotation
*/
class Lexicographically
{
func minimumString(_ text: String)
{
let n: Int = text.count;
if (n <= 0)
{
return;
}
let auxiliary: String = text + text;
// Use to collect possible rotation
var record: [String] = Array(repeating: "", count: n);
var i: Int = 0;
// Collect the possible rotation
while (i < n)
{
let start =
auxiliary.index(auxiliary.startIndex, offsetBy: i)
let end =
auxiliary.index(auxiliary.endIndex, offsetBy:-(n-i))
let range = start..<end;

let data = auxiliary[range]  ;
record[i] = String(data);
i += 1;
}
// Sort record element
record = record.sorted();
// Display given string
print(" Given  : ", text);
// Display result
print(" Result : ", record[0]);
}
}
func main()
{
// Test
/*
ATMAMOUNT
TMAMOUNTA
MAMOUNTAT
AMOUNTATM
MOUNTATMA
OUNTATMAM
UNTATMAMO
NTATMAMOU
TATMAMOUN
----------
AMOUNTATM  <- Result

*/
/*
LANGUAGE
ANGUAGEL
NGUAGELA
GUAGELAN
UAGELANG
AGELANGU
GELANGUA
ELANGUAG
--------
AGELANGU   <- Result

*/
/*
EBILL
BILLE
ILLEB
LLEBI
LEBIL
--------
BILLE   <- Result

*/
}
main();``````

Output

`````` Given  :  ATMAMOUNT
Result :  AMOUNTATM
Given  :  LANGUAGE
Result :  AGELANGU
Given  :  EBILL
Result :  BILLE``````
``````/*
Kotlin program for
Lexicographically minimum string rotation
*/
class Lexicographically
{
fun minimumString(text: String): Unit
{
val n: Int = text.length;
if (n <= 0)
{
return;
}
val auxiliary: String = text + text;
// Use to collect possible rotation
val record: Array < String > = Array(n)
{
""
};
var i: Int = 0;
// Collect the possible rotation
while (i < n)
{
record[i] = auxiliary.substring(i, i + n);
i += 1;
}
// Sort record element
record.sort();
// Display given string
println(" Given  : " + text);
// Display result
println(" Result : " + record[0]);
}
}
fun main(args: Array < String > ): Unit
{
// Test
/*
ATMAMOUNT
TMAMOUNTA
MAMOUNTAT
AMOUNTATM
MOUNTATMA
OUNTATMAM
UNTATMAMO
NTATMAMOU
TATMAMOUN
----------
AMOUNTATM  <- Result

*/
/*
LANGUAGE
ANGUAGEL
NGUAGELA
GUAGELAN
UAGELANG
AGELANGU
GELANGUA
ELANGUAG
--------
AGELANGU   <- Result

*/
/*
EBILL
BILLE
ILLEB
LLEBI
LEBIL
--------
BILLE   <- Result

*/
}``````

Output

`````` Given  : ATMAMOUNT
Result : AMOUNTATM
Given  : LANGUAGE
Result : AGELANGU
Given  : EBILL
Result : BILLE``````

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post