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 for Left Rotation and Right Rotation of a String
To implement left rotation and right rotation of a string, we can use various approaches. Left rotation means moving characters counter-clockwise by a given number of positions, while right rotation means moving characters clockwise by a given number of positions.
In this article we have a string and a value of k by which we will rotate the string. Our task is to write a JavaScript program for left rotation and right rotation of a string.
Example
Input: String str = "apple"; k = 3 Output: Left Rotation: "leapp" Right Rotation: "pleap"
Approaches for Left and Right Rotation of String
Here are the approaches for left rotation and right rotation of a string in JavaScript which we will discuss with stepwise explanations and complete example codes.
Using String Slicing
For left rotation and right rotation of string, we extract a part of the string and concatenate it with the remaining string by the value of k.
- First we get the given string in a variable str and store the number of rotations required in another variable k.
- We create two functions, left_rotation() for the left rotation and right_rotation() for the right rotation of the string. Both functions accept two arguments, str and k.
- In the left rotation function, we get two substrings using slice() method and switch their places.
- In the right rotation function, we get two substrings and switch their positions for clockwise movement.
Example
var str = "apple";
var k = 3;
console.log("The given string is: " + str);
function left_rotation(str, k) {
k = k % str.length;
var new_str = str.slice(k) + str.slice(0, k);
console.log("String after " + k + " left rotation is: " + new_str);
}
function right_rotation(str, k) {
k = k % str.length;
var new_str = str.slice(str.length - k) + str.slice(0, str.length - k);
console.log("String after " + k + " right rotation is: " + new_str);
}
left_rotation(str, k);
right_rotation(str, k);
The given string is: apple String after 3 left rotation is: leapp String after 3 right rotation is: pleap
Using String Concatenation
In this approach for left rotation and right rotation of string we concatenate the string with itself to create a doubled string, then extract the required substring.
- First we get the given string in a variable str and store the number of rotations required in another variable k.
- We create two functions, leftRotate() for the left rotation and rightRotate() for the right rotation of the string.
- Inside leftRotate function, we use modulo operator to ensure "k" does not exceed the string length.
- Then concatenate the string with itself and store it in variable doubled.
- The substr() function gets the string from "k" to string length for left rotation.
- For right rotation, we extract substring from position (str.length - k).
Example
var str = "apple";
var k = 3;
console.log("The given string is: " + str);
function leftRotate(str, k) {
k = k % str.length;
const doubled = str + str;
return doubled.substr(k, str.length);
}
function rightRotate(str, k) {
k = k % str.length;
const doubled = str + str;
return doubled.substr(str.length - k, str.length);
}
console.log("String after " + k + " left rotation is: " + leftRotate(str, k));
console.log("String after " + k + " right rotation is: " + rightRotate(str, k));
The given string is: apple String after 3 left rotation is: leapp String after 3 right rotation is: pleap
Using Recursive Rotation
In this approach we use recursive rotation where we call both functions until we achieve the desired result by rotating one character at a time.
- We create two functions, leftRotate() for the left rotation and rightRotate() for the right rotation.
- Inside the leftRotate() function we set the terminating condition when k==0.
- When it is not the terminating condition, we move the first element of string to end and concatenate with sliced string.
- The function calls itself recursively until k reaches 0.
- Similarly, we repeat the process for right rotation in rightRotate() function.
Example
var str = "apple";
var k = 3;
console.log("The given string is: " + str);
function leftRotate(str, k) {
if (k === 0) return str;
var new_str = leftRotate(str.slice(1) + str[0], k - 1);
return new_str;
}
function rightRotate(str, k) {
if (k === 0) return str;
var new_str = rightRotate(str[str.length - 1] + str.slice(0, -1), k - 1);
return new_str;
}
console.log("String after " + k + " left rotation is: " + leftRotate(str, k));
console.log("String after " + k + " right rotation is: " + rightRotate(str, k));
The given string is: apple String after 3 left rotation is: leapp String after 3 right rotation is: pleap
Complexity Comparison
Here is a comparison of time and space complexity of all the above approaches.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| String Slicing | O(n) | O(n) |
| String Concatenation | O(n) | O(n) |
| Recursive Rotation | O(n*k) | O(k) |
Conclusion
We have implemented JavaScript programs for left rotation and right rotation of strings using three different approaches: string slicing, string concatenation, and recursive rotation. String slicing is the most efficient approach for practical use cases.
