Python – Right and Left Shift characters in String
Last Updated :
08 May, 2023
Sometimes, while working with Python Strings, we can have problem in which we have both right and left rotate count of characters in String and would like to know the resultant condition of String. This type of problem occurs in competitive programming. Lets discuss certain ways in which this task can be performed.
Method #1 : Using String multiplication + string slicing
The combination of above functions can be used to perform this task. In this, we multiply the string thrice, perform the concatenation and selectively slice string to get required result.
Python3
test_str = 'geeksforgeeks'
print ( "The original string is : " + test_str)
r_rot = 7
l_rot = 3
res = (test_str * 3 )[ len (test_str) + r_rot - l_rot:
2 * len (test_str) + r_rot - l_rot]
print ( "The string after rotation is : " + str (res))
|
Output :
The original string is : geeksforgeeks
The string after rotation is : sforgeeksgeek
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using % operator and string slicing
The combination of the above functionalities can also be used to perform this task. In this, we find the mod of rotation difference with length to compute the string position.
Python3
test_str = 'geeksforgeeks'
print ( "The original string is : " + test_str)
r_rot = 7
l_rot = 3
temp = (r_rot - l_rot) % len (test_str)
res = test_str[temp:] + test_str[: temp]
print ( "The string after rotation is : " + str (res))
|
Output :
The original string is : geeksforgeeks
The string after rotation is : sforgeeksgeek
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using list slicing and join
this approach uses list slicing and join to rotate the string. It converts the input string to a list of characters, slices the list into two parts, concatenates them to get the rotated list, and then joins the list back into a string to get the rotated string.
Algorithm:
- Convert the input string into a list of characters.
- Slice the list into two parts, one from the starting index to the number of characters to be shifted and the other from the number of characters to be shifted to the end of the list.
- Concatenate the second part with the first part to get the rotated list.
- Join the list into a string to get the rotated string.
- Print the rotated string.
Python3
def left_shift_string(string, n):
char_list = list (string)
rotated_list = char_list[n:] + char_list[:n]
rotated_string = "".join(rotated_list)
return rotated_string
def right_shift_string(string, n):
char_list = list (string)
rotated_list = char_list[ - n:] + char_list[: - n]
rotated_string = "".join(rotated_list)
return rotated_string
string = "geeksforgeeks"
n = 3
left_rotated_string = left_shift_string(string, n)
right_rotated_string = right_shift_string(string, n)
print ( "Original string:" , string)
print ( "Left rotated string:" , left_rotated_string)
print ( "Right rotated string:" , right_rotated_string)
|
Output
Original string: geeksforgeeks
Left rotated string: ksforgeeksgee
Right rotated string: eksgeeksforge
Time complexity: O(n), where n is the length of the string.
Auxiliary Space: O(n), as we are creating a list of characters to store the rotated list.
Approach 4: Using lambda function
- We define a lambda function called rotate that performs the string rotation operation.
- The lambda function takes a string s as its input and returns the rotated string.
- We then use the lambda function rotate to perform the string rotation operation on the input string test_str. Finally, we print the rotated string res.
Below is the code for the above approach is as follows:
Python3
test_str = 'geeksforgeeks'
print ( "The original string is : " + test_str)
r_rot = 7
l_rot = 3
rotate = lambda s: (s * 3 )[ len (s) + r_rot - l_rot : 2 * len (s) + r_rot - l_rot]
res = rotate(test_str)
print ( "The string after rotation is : " + str (res))
|
Output
The original string is : geeksforgeeks
The string after rotation is : sforgeeksgeek
Time Complexity: O(n), where n is the length of the input string test_str.
Auxiliary Space: O(n), as we are creating a new string with length 3n using string multiplication.
Approach #5: Using List comprehension and string concatenation
We can use a list comprehension to shift the characters in the string and then concatenate the resulting characters to form the rotated string.
Steps:
- Initialize an empty list to store the shifted characters.
- Use a list comprehension to shift the characters to the right or left based on the rotation value.
- Concatenate the shifted characters to form the rotated string.
- Return the rotated string.
Python3
test_str = 'geeksforgeeks'
print ( "The original string is : " + test_str)
r_rot = 7
l_rot = 3
res = ''.join([test_str[(i + r_rot - l_rot) % len (test_str)]
for i in range ( len (test_str))])
print ( "The string after rotation is : " + res)
|
Output
The original string is : geeksforgeeks
The string after rotation is : sforgeeksgeek
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), where n is the length of the input string.
Please Login to comment...