Find Lexicographically Minimum String Rotation in JavaScript



To find lexicographically minimum string rotation in Javascript, we will understand two different approaches in this article. A lexicographically minimum rotation of a string is the smallest string that can be obtained by rotating the original string any number of times in lexicographical order. By lexicographical order it means dictionary order such as: a < b, ab < ac.

In this article we are having a string. Our task is to write a JavaScript program to find lexicographically minimum string rotation.

Example

Input: bba

All lexicographical order are:
bba, bab, abb

Output:
Smallest lexicographical rotation: abb

Approaches for Lexicographically Minimum String Rotation

Here is a list of approaches to find lexicographically minimum string rotation in Javascript which we will be discussing in this article with stepwise explanation and complete example codes.

Using Brute Force Approach

To find lexicographically minimum string rotation we have used brute force approach where we generate all possible rotations of the string, store them in an array, and then sort the array to find the smallest rotation.

  • Generate all possible rotations of the string. We have used for loop to traverse, slice() method to extract the part of string and then concatenated them.
  • Store these rotations in an array using push() method.
  • Sort the array lexicographically using sort() method.
  • Return the first element in the sorted array, the smallest rotation.
  • The minimum string rotation is displayed in web console using console.log() method.

Example

Below is an example to find the Lexicographically minimum string rotation using brute force approach.

const str = "bba";
console.log("Input string:", str);
function minRotation(str) {
    const n = str.length;
    const rotations = [];
    for (let i = 0; i 

Time Complexity: O(n^2 log n) due to generating n rotations of length n and sorting them.

Space Complexity:O(n^2) for storing all rotations in the array.

Using Double String

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.

  • Concatenate the original string with itself to ensure 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

Below is an example to find the Lexicographically minimum string rotation:

const str = 'bba';
console.log("Input string:", str);

function minRotation(str) {
    let double = str + str;
    let len = str.length;
    let rotation = double.substring(0, len);
    for (let i = 1; i 

Time Complexity: O(n^2) due to substring extraction and comparisons for n rotations.

Space Complexity: O(n) for the concatenated string and temporary variables.

Complexity Comparison

Here is a comparison of time and space complexity of all the above approaches.

Approach Time Complexity Space Complexity
Brute Force O(n^2 log n) O(n^2)
Double String O(n^2) O(n)

Conclusion

In this article to find lexicographically minimum string rotation we have used two different approaches. Both the approaches Brute force and double string approaches effectively find the lexicographically minimum string rotation, each with distinct strengths.

Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.
Updated on: 2025-01-20T17:45:47+05:30

530 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements