JavaScript Program for Left and Right Rotation of a String



To implement left rotation and right rotation of a string, we can use various approaches to implement this problem. Left rotation of a string means anti-clockwise movement of the given number of characters and right rotation of the string means clockwise movement of the given number of characters.

In this article we are having 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 is a list of approaches for left rotation and right rotation of a string in JavaScript which we will be discussing in this article with stepwise explanation and complete example codes.

Using String Slicing

For left rotation and right rotation of string, we will be extracting a part of the string and then concatenating it with remaining string by the value of k.

  • First we will get the given string in a variable str and store the number of rotation required in another variable k. We can print them also to get a better comparison using console.log() method.
  • We will 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 will get the two substrings using slice() method that contains the last k element and second the remaining elements and switch their places.
  • In the right rotation function, we will get the two substring that contains the first k element and the second with remaining elements and switch their places.
  • Then we have call both the functions.

Example

Here is a complete example code implementing above mentioned steps for left rotation and right rotation of a string in JavaScript using string slicing.

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);

Using String Concatenation

In this approach for left rotation and right rotation of string we have self concatenated the string with itself.

  • First we will get the given string in a variable str and store the number of rotation required in another variable k. We can print them also to get a better comparison using console.log() method.
  • We will create two functions, leftRotate() for the left rotation and rightRotate() for the right rotation of the string. Both functions accept two arguments, str and k.
  • Inside leftRotate function, we have used length property to make sure "k" does not exceed the string length.
  • Then concatenated the string with itself and stored it in variable double.
  • At the end the substr() function gets the string from "k" to string length. This result is returned.
  • Similarly we have used above methods in rightRotate() function and at the end the function is called and displayed in web console using console.log() method.

Example

Here is a complete example code implementing above mentioned steps for left rotation and right rotation of a string in JavaScript using string concatenation.

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));

Using Recursive Rotation

In this approach for left rotation and right rotation of string we have have used recursive rotation. We call both the functions till we achieve the desired result.

  • First we will get the given string in a variable str and store the number of rotation required in another variable k. We can print them also to get a better comparison using console.log() method.
  • We will create two functions, leftRotate() for the left rotation and rightRotate() for the right rotation of the string. Both functions accept two arguments, str and k.
  • Inside the leftRotate() function we have set the terminating condition i.e. when k==0. On reaching the terminating condition, the string is returned.
  • When it is not terminating condition then, we move the first element of string to end and concatenate with sliced string str.slice(1).
  • Then the leftRotate() function is called recursively and the new_str is returned.
  • Similarly, repeat the above process for right rotation in rightRotate() function.
  • At the end, both the functions are called and the result is displayed in web console using console.log() method.

Example

Here is a complete example code implementing above mentioned steps for left rotation and right rotation of a string in JavaScript using recursive rotation.

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));

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(1)
String Concatenation O(n) O(n)
Recursive Rotation O(n) O(n)

Conclusion

In this article, we have implemented the JavaScript program for the left rotation and right rotation of a given string. We have used three different approaches which are by using string slicing, string concatenation and recursive rotation approach. Any of the approaches can be used based on the user preference.

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-21T12:53:37+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements