Given a string of length n, rotate the string either, Left (anticlockwise) by d positions or Right (clockwise) by d positions where 0 ≤ d ≤ n. For Example:
Input: "GeeksforGeeks", d=2
Output: Left Rotation: "eksforGeeksGe"
Right Rotation: "ksGeeksforGee"
Let's discuss different ways to rotate a string in Python.
Using String Slicing
This method rotates a string by cutting it at a given position and then joining the two parts in reverse order to form the rotated result.
s = "GeeksforGeeks"
d = 2
left = s[d:] + s[:d]
right = s[-d:] + s[:-d]
print("Left rotation:", left)
print("Right rotation:", right)
Output
Left rotation: eksforGeeksGe Right rotation: ksGeeksforGee
Explanation:
- left = s[d:] + s[:d]: for left rotation take the substring from index d to end (s[d:]) and add the first d characters (s[:d]).
- right = s[-d:] + s[:-d]: for right rotation take the last d characters (s[-d:]) and add everything before them (s[:-d]).
By Extending the String
This method doubles the string (s + s) so all possible rotations appear inside it. Then the rotated version is obtained by slicing the extended string at the correct position.
s = "GeeksforGeeks"
d = 2
ext = s + s
n = len(s)
left = ext[d : d + n]
right = ext[n - d : 2*n - d]
print("Left Rotation:", left)
print("Right Rotation:", right)
Output
Left Rotation: eksforGeeksGe Right Rotation: ksGeeksforGee
Explanation:
- ext = s + s: doubling the string contains all rotations.
- Left rotation: ext[d : d + n]: slice starting at d for n characters.
- Right rotation: ext[n - d : 2*n - d]: slice starting d chars before n for n characters.
Rotation Using Collections.deque
This method converts the string into a deque (double-ended queue) so that characters can be rotated directly using the built-in rotate() function, avoiding manual slicing and making rotation efficient.
from collections import deque
s = "GeeksforGeeks"
d = 2
dq = deque(s)
# Left Rotation
dq.rotate(-d)
left = ''.join(dq)
# Right Rotation
dq.rotate(d) # undo then rotate right
dq.rotate(d)
right = ''.join(dq)
print("Left Rotation:", left)
print("Right Rotation:", right)
Output
Left Rotation: eksforGeeksGe Right Rotation: ksGeeksforGee
Explanation:
- dq = deque(s): converts the string into a deque so we can rotate efficiently.
- dq.rotate(-d): negative value rotates left; collect the left-rotated string.
- dq.rotate(d): reverses the previous rotation.
- dq.rotate(d) again: rotates right by d; collect the right-rotated string.
- ''.join(dq): converts the deque back to a string.
Using For Loop
This approach rotates the string by rearranging its characters step by step using loops, so the shifting process can be seen clearly instead of happening automatically through slicing or built-in functions.
s = "GeeksforGeeks"
d = 2
n = len(s)
# Left Rotation
left = ""
for i in range(d, n):
left += s[i]
for i in range(d):
left += s[i]
# Right Rotation
right = ""
for i in range(n - d, n):
right += s[i]
for i in range(n - d):
right += s[i]
print("Left Rotation:", left)
print("Right Rotation:", right)
Output
Left Rotation: eksforGeeksGe Right Rotation: ksGeeksforGee
Explanation:
- for i in range(d, len(s)): loops from index d -> end, adding all remaining characters.
- for i in range(d): appends the first d characters to complete the left rotation.
- for i in range(n - d, n): adds the last d characters first for right rotation.
- for i in range(n - d): appends the rest of the characters, finishing the right rotation.