Open In App

Javascript Program for Left Rotation and Right Rotation of a String

Last Updated : 19 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a string of size n, write functions to perform the following operations on a string-

  1. Left (Or anticlockwise) rotate the given string by d elements (where d <= n)
  2. Right (Or clockwise) rotate the given string by d elements (where d <= n).

Examples: 

Input : s = "GeeksforGeeks"
d = 2
Output : Left Rotation : "eksforGeeksGe"
Right Rotation : "ksGeeksforGee"


Input : s = "qwertyu"
d = 2
Output : Left rotation : "ertyuqw"
Right rotation : "yuqwert"

Method 1:

A Simple Solution is to use a temporary string to do rotations. For left rotation, first, copy last n-d characters, then copy first d characters in order to the temporary string. For right rotation, first, copy last d characters, then copy n-d characters. 

Can we do both rotations in-place and O(n) time? 

The idea is based on a reversal algorithm for rotation.

// Left rotate string s by d (Assuming d <= n)
leftRotate(s, d)
reverse(s, 0, d-1); // Reverse substring s[0..d-1]
reverse(s, d, n-1); // Reverse substring s[d..n-1]
reverse(s, 0, n-1); // Reverse whole string.

// Right rotate string s by d (Assuming d <= n)
rightRotate(s, d)

// We can also call above reverse steps
// with d = n-d.
leftRotate(s, n-d)

Below is the implementation of the above steps : 

JavaScript
// JavaScript program for Left Rotation and Right
// Rotation of a String

// Function that rotates s towards left by d
function leftrotate(str, d) {
    let ans = str.substring(d, str.length) +
        str.substring(0, d);
    return ans;
}

// Function that rotates s towards right by d
function rightrotate(str, d) {
    return leftrotate(str, str.length - d);
}

// Driver code
let str1 = "GeeksforGeeks";
console.log(leftrotate(str1, 2));

let str2 = "GeeksforGeeks";
console.log(rightrotate(str2, 2));

Output: 

Left rotation:  eksforGeeksGe
Right rotation: ksGeeksforGee

Complexity Analysis:

  • Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time 
  • Auxiliary Space: O(1), as we are not using any extra space.

Method 2:

We can use extended string which is double in size of normal string to rotate string. For left rotation, access the extended string from index n to the index len(string) + n. For right rotation, rotate the string left with size-d places.

Approach:

// Left rotate string s by d 
leftRotate(s, n)
temp = s + s; // extended string
l1 = s.length // length of string
return temp[n : l1+n] //return rotated string.

// Right rotate string s by n
rightRotate(s, n)
// We can also call above reverse steps
// with x = s.length - n.
leftRotate(s, x-n)

Below is implementation of above approach:

JavaScript
// JavaScript program for Left Rotation and Right
// Rotation of a String

// Function that rotates string towards left by n
function leftrotate(str1, n)
{
    var temp = str1 + str1;
    var l1 = str1.length;
    
    var Lfirst = temp.substr(n,l1);

//     now returning string
    return Lfirst;
}

// Function that rotates string towards right by n
function rightrotate(str, d)
{
    return leftrotate(str, str.length - d);
}

// Driver code
var str1 = "GeeksforGeeks";
console.log(leftrotate(str1, 2));

var str2 = "GeeksforGeeks";
console.log(rightrotate(str2, 2) );

// This code is contributed by Susobhan Akhuli

Output
eksforGeeksGe
ksGeeksforGee

Complexity Analysis:

  • Time Complexity: O(N), where N is the size of the given string.
  • Auxiliary Space: O(N)

Please refer complete article on Left Rotation and Right Rotation of a String for more details!



Next Article
Practice Tags :

Similar Reads