Left Rotation and Right Rotation of a StringIn this article, we will look at how to implement left and right string rotation in Python. The stepbystep 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 StringLeft 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 leftrotating a string are as follows:
To summarize, for a string of length L:
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. Leftrotating 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 ApproachExplanation
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 RecursionExplanation
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 RotationThe 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 rightrotate a string are:
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. ImplementationThere are two ways to implement the right rotation: Iterative
The iterative approach rotates the string in O(L) time and space. Recursive
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 ApproachExplanation
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 ApproachExplanation
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.
Next TopicMerge Two Sorted Linked Lists
