# JavaScript Program to Find Lexicographically minimum string rotation

We will be finding the lexicographically minimum string rotation in JavaScript. The method involves concatenating the original string with itself and then using the built-in 'sort' function to sort the concatenated string in ascending order. Finally, we will be returning the smallest substring of the sorted concatenated string which has the same length as the original string. This will be the lexicographically minimum string rotation.

We will be implementing this logic by making use of string manipulation techniques and built-in functions available in JavaScript. The result of our implementation will be a string that represents the lexicographically minimum rotation of the input string. This will be useful for comparing and sorting strings in an efficient manner.

In the future, we will continue to improve the algorithm to make it faster and more efficient for finding the lexicographically minimum string rotation.

## Approach

Here is an explanation of the approach to find the lexicographically minimum string rotation in 5 lines −

• Concatenate the original string with itself to make sure all possible rotations are considered.

• Find the first character that is not equal to its next character, which will be the starting point of the minimum rotation.

• If no such character is found, return the original string since it is already the minimum rotation.

• Return the substring of the concatenated string starting from the found character to the end of the string as the minimum rotation.

• The resulting substring will be the lexicographically minimum string rotation.

## Example

A lexicographically minimum string rotation can be found by concatenating the original string with itself and finding the smallest substring that starts with the first character of the original string.

Here is an example implementation of this in JavaScript −

function findLexicographicallyMinimumStringRotation(str) {
let strDouble = str + str;
let len = str.length;
let minRotation = strDouble.substring(0, len);
for (let i = 1; i < len; i++) {
let currRotation = strDouble.substring(i, i + len);
if (currRotation < minRotation) {
minRotation = currRotation;
}
}
return minRotation;
}
const str = 'eadbc';
console.log(findLexicographicallyMinimumStringRotation(str));


## Explanation

• First, we concatenate the original string with itself to get strDouble.

• We also define a variable len to store the length of the original string.

• We then initialize minRotation with the first substring of length len from strDouble, which is strDouble.substring(0, len). This is our starting point for finding the lexicographically minimum string rotation.

• We then use a for loop to iterate over all the possible substrings of length len in strDouble, starting from the second character.

• For each iteration, we find the current rotation currRotation by taking a substring of length len from strDouble, starting from the current position i.

• If currRotation is smaller than minRotation, we update minRotation with the current rotation.

• Finally, after the for loop has finished, we return the value of minRotation, which is the lexicographically minimum string rotation.

Updated on: 15-Mar-2023

224 Views

##### Kickstart Your Career

Get certified by completing the course

Advertisements