Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program to Find Lexicographically minimum string rotation
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 = [];
// Generate all rotations
for (let i = 0; i < n; i++) {
let rotation = str.slice(i) + str.slice(0, i);
rotations.push(rotation);
console.log("Rotation", i + ":", rotation);
}
// Sort lexicographically and return the first
rotations.sort();
return rotations[0];
}
console.log("Lexicographically Minimum Rotation:", minRotation(str));
Input string: bba Rotation 1: bba Rotation 2: bab Rotation 3: abb Lexicographically Minimum Rotation: abb
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 of original length. This approach eliminates the need to store all rotations.
- Concatenate the original string with itself to ensure all possible rotations are considered as substrings.
- Iterate through each possible starting position and extract substrings of original length.
- Compare each substring with the current minimum rotation found.
- Return the lexicographically smallest substring as the minimum rotation.
- This approach is more space-efficient than storing all rotations.
Example
Below is an example to find the Lexicographically minimum string rotation using double string approach:
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);
console.log("Double string:", double);
console.log("Initial rotation:", rotation);
for (let i = 1; i < len; i++) {
let currRotation = double.substring(i, i + len);
console.log("Checking rotation:", currRotation);
if (currRotation < rotation) {
rotation = currRotation;
console.log("New minimum found:", rotation);
}
}
return rotation;
}
console.log("Lexicographically Minimum Rotation:", minRotation(str));
Input string: bba Double string: bbabba Initial rotation: bba Checking rotation: bab Checking rotation: abb New minimum found: abb Lexicographically Minimum Rotation: abb
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.
Comparison
Here is a comparison of time and space complexity of both approaches:
| Approach | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Brute Force | O(n^2 log n) | O(n^2) | Understanding concept |
| Double String | O(n^2) | O(n) | Better performance |
Conclusion
Both approaches effectively find the lexicographically minimum string rotation. The double string method offers better space complexity O(n) compared to brute force O(n^2), making it more suitable for larger strings.
