VOOZH about

URL: https://www.geeksforgeeks.org/dsa/left-rotation-right-rotation-string-2/

⇱ Left Rotation of a String - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Left Rotation of a String

Last Updated : 11 Nov, 2024

Given a string s and an integer d, the task is to left rotate the string by d positions.

Examples:

Input: s = "GeeksforGeeks", d = 2
Output: "eksforGeeksGe"
Explanation: After the first rotation, string s becomes "eeksforGeeksG" and after the second rotation, it becomes "eksforGeeksGe".

Input: s = "qwertyu", d = 2
Output: "ertyuqw"
Explanation: After the first rotation, string s becomes "wertyuq" and after the second rotation, it becomes "ertyuqw".

[Naive Approach] Left Rotate one by one

The idea is to store the first character in a variable and shift all the remaining characters to the left by one position, then place the first character at the end of string. This process is repeated d times.


Output
eksforGeeksGe

Time Complexity: O(n*d), the outer loop runs d times, and inner loop runs n times.
Auxiliary Space: O(1) if the string is mutable, like in C++. For immutable strings like in Java, C#, Python and Javascript an extra character array of size n is used, so the space complexity will be O(n).

[Better Approach] Using Temporary Char Array

The idea is to use a temporary character array of size n (size of original string). If we left rotate the string by d positions, the last n – d elements will be at the front and the first d elements will be at the end.

  • Copy the last (n – d) elements of original string into the first n – d positions of temporary array.
  • Then, copy the first d elements of the original string to the end of temporary array.
  • Finally, convert the temporary char array to the string.

Output
eksforGeeksGe

Time Complexity: O(n), as we are visiting each element only twice.
Auxiliary Space: O(n), as we are using an additional character array.

[Expected Approach - 1] Using Juggling Algorithm

The idea behind the juggling algorithm is that we can rotate all the elements in cycle. Each cycle is independent and represents a group of elements that will shift among themselves during the rotation. If the starting index of a cycle is i, then next elements of the cycle will be present at indices (i + d) % n, (i + 2d) % n, (i + 3d) % n ... and so on till we reach back to index i. The total number of cycles will be GCD of n and d. And, we perform a single left rotation within each cycle.

To know more about the Juggling algorithm, refer this article - Juggling Algorithm for Array Rotation.


Output
eksforGeeksGe

Time Complexity: O(n)
Auxiliary Space: O(1) if the string is mutable, like in C++. For immutable strings like in Java, C#, Python and Javascript an extra character array of size n is used, so the space complexity will be O(n).

[Expected Approach - 2] Using Reversal Algorithm

The idea is based on the observation that if we left rotate the string by d positions, the last (n – d) elements will be at the front and the first d elements will be at the end.

  • Reverse the substring containing the first d elements of the string.
  • Reverse the substring containing the last (n – d) elements of the string.
  • Finally, reverse all the elements of the string.

Output
eksforGeeksGe

Time Complexity: O(n), where n is the size of the given string.
Auxiliary Space: O(1) if the string is mutable, like in C++. For immutable strings like in Java, C#, python and Javascript, an extra character array of size n is used, so the space complexity will be O(n).

Comment
Article Tags: