# Left Rotation and Right Rotation of a String

In this article, we will look at how to implement left and right string rotation in Python. The step-by-step algorithms and code examples demonstrate the mechanics of rotating strings in either direction. We also discuss use cases and applications where string rotation is useful. The linear time complexity ensures that even large strings are rotated fast. It would help if you had a good grasp of this fundamental string manipulation operation by the end.

Strings are fundamental data structures used in many computer science and programming areas. Often, we need to manipulate strings by rearranging the order of characters. One such operation is string rotation, where the characters in a string are rotated left or right by several positions. In left rotation, characters are shifted to the left, while right rotation shifts them to the right.

String rotation has applications in several domains, including cryptography, recursion programming exercises, and sequence rearrangement. Cryptographic algorithms like ROT13 and Caesar cypher use rotation to encrypt and decrypt text. In coding interviews and online practice, string rotation often becomes an algorithmic problem to test a programmer's skills

## Left Rotation of the String

Left rotation is an operation where each character in a string is shifted to the left by a specified number of positions. For example, if we left to rotate the string "Hello" by 2 positions, it becomes "love".

The mechanics of left-rotating a string are as follows:

1. First, we must determine n - the number of positions to rotate the string by. For our example, n is 2.
2. Next, we take the first n characters from the original string and append them to the end of the string. For "Hello", the first 2 characters are "He".
3. Now we append "He" to the end of the string, making it "love". The first n characters have now been rotated to the end.
4. Finally, we remove the first n characters "He" from the original string. This leaves us with the final left rotated string "lloHe".

To summarize, for a string of length L:

• Take the first n characters and append them to the end
• Remove the first n characters

This shifts every character in the string to the left by n positions.

The time complexity is O(L) since each character in the string is shifted once. The space complexity is O(L) as well since, at most, we create a copy of the string of length L.

Left-rotating strings have applications in cryptography. For example, the ROT13 cypher rotates every alphabet letter by 13 positions. This simple cipher was used to protect communication.

So, in essence, left rotation rearranges a string by cyclically shifting characters to the left. The linear time and space complexity makes it fast, even on large inputs.

### Python Implementation: Simple Approach

Explanation

• Define a function left_rotate that takes the string and number of positions to rotate (n) as arguments.
• Calculate the length of the string using len(). This is needed later to handle edge cases.
• Take modulo n with length L to restrict n between 0 to L-1. This prevents edge case errors.
• Slice the string into two parts - left_part from 0 to n and right_part from n to end.
• Concatenate right_part and left_part to get the rotated string.
• Return the rotated string.
• Call left_rotate by passing the string and n. Print the original and rotated string.

The time complexity is O(L) to slice the string. Space complexity is O(L) to store the sliced strings. This handles rotation on any string in linear time.

### Using Recursion

Explanation

• The base case is when n reaches 0; we simply return the string.
• Calculate the length L of string to handle edge cases.
• Take modulo n with L to restrict n between 0 and L-1.
• Recursively call left rotate by slicing off the first character and appending it to the end.
• Decrement n at each call until the base case is reached.
• Return the rotated string.

At each call, one character is moved from start to end. After n calls, the string is rotated left by n positions.

The time complexity is O(n) since we recurse n times. Space complexity is O(n) for the recursion stack.

This demonstrates how we can recursively rotate a string using string slicing. The linear time and space complexity makes it efficient.

## Right Rotation

The right rotation is similar to the left rotation, except the characters are shifted to the right instead of the left.

For a string "Hello", right rotating it by 2 positions will result in "local". The last 2 characters, "Lo", are moved to the start, and "Lo" is removed from the end.

The steps to right-rotate a string are:

1. Determine n - the number of positions to rotate by.
2. Take the last n characters and prepend them to the start of the string.
3. Remove the last n characters from the original string.

This shifts all characters in the string to the right by n positions.

The time and space complexity is O(L), where L is the length of the string.

### Implementation

There are two ways to implement the right rotation:

Iterative

1. Find the length L of the string
2. Take modulo n with L to restrict n between 0 to L-1
3. Slice the last n characters and prepend to start
4. Remove the last n characters from the original string
5. Return rotated string

The iterative approach rotates the string in O(L) time and space.

Recursive

1. Base case: If n is 0, return the string
2. Take the last character and prepend it to a recursive call on the string, excluding the last character.
3. Decrement n and recurse until n is 0
4. Return rotated string

The recursive approach also rotates in O(L) time and space by recursively prepending the last character.

So, in summary, right rotation shifts the characters to the right rather than the left. Both iterative and recursive implementations have linear time and space complexity.

### Python Implementation: Iterative Approach

Explanation

1. Define the right_rotate function that takes the string and number of rotations n as parameters.
2. Calculate the length L of the string using len(). This will be used later.
3. Take modulo n with L using n = n % L. This ensures n is restricted between 0 and L-1, preventing index out-of-range errors.
4. Slice the last n characters of the string into a substring called right using str[-n:].
5. Slice the remaining characters to the left into a substring called left using str[:-n].
6. Concatenate right and left substrings to get the rotated string.
7. Return the rotated string.
8. Initialize the example string "Hello" and n = 2.
9. Print the original string.
10. Call right_rotate(), passing the string and n. Print the returned rotated string.

This steps through the string once, making it O(L) in time and space complexity. The modular arithmetic handles errors for large n values. Overall, it provides a clean iterative implementation of right string rotation.

### Recursive Approach

Explanation

1. Define the right_rotate function, taking the string and n as arguments.
2. Base case - If n becomes 0, just return the string.
3. Calculate the length L of the string to handle edge cases.
4. Take modulo n with L to restrict n between 0 and L-1.
5. Recursively call right_rotate while prepending the last character using str[-1].
6. Slice the string to exclude the last character using str[:-1] before recursing.
7. Decrement n at each recursive call to move towards the base case.
8. When base case n == 0 is reached, unwind and return the rotated string.
9. Initialize the string "Hello" and n = 2.
10. Print the original string.
11. Call right_rotate() passing string and n. Print returned string.

This recursively shifts each character to the right until n becomes 0. The time and space complexity is O(n) as we recurse n times in the worst case.