Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Right and Left Shift characters in String

Python – Right and Left Shift characters in String

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




# Python3 code to demonstrate working of
# Right and Left Shift characters in String
# Using String multiplication + string slicing
 
# Initializing string
test_str = 'neveropen'
 
# Printing original string
print("The original string is : " + test_str)
 
# Initializing right rot
r_rot = 7
 
# Initializing left rot
l_rot = 3
 
# Right and Left Shift characters in String
# Using String multiplication + string slicing
res = (test_str * 3)[len(test_str) + r_rot - l_rot:
                     2 * len(test_str) + r_rot - l_rot]
 
# Printing result
print("The string after rotation is : " + str(res))


Output : 

The original string is : neveropen
The string after rotation is : sforLazyroargeek

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




# Python3 code to demonstrate working of
# Right and Left Shift characters in String
# Using % operator and string slicing
 
# Initializing string
test_str = 'neveropen'
 
# Printing original string
print("The original string is : " + test_str)
 
# Initializing right rot
r_rot = 7
 
# Initializing left rot
l_rot = 3
 
# Right and Left Shift characters in String
# Using % operator and string slicing
temp = (r_rot - l_rot) % len(test_str)
res = test_str[temp:] + test_str[: temp]
 
# Printing result
print("The string after rotation is : " + str(res))


Output : 

The original string is : neveropen
The string after rotation is : sforLazyroargeek

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:

  1. Convert the input string into a list of characters.
  2. 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.
  3. Concatenate the second part with the first part to get the rotated list.
  4. Join the list into a string to get the rotated string.
  5. 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 = "neveropen"
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: neveropen
Left rotated string: ksforLazyroargee
Right rotated string: eksLazyroarforge

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

  1. We define a lambda function called rotate that performs the string rotation operation.
  2. The lambda function takes a string s as its input and returns the rotated string.
  3. 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




# Python3 code to demonstrate working of
# Right and Left Shift characters in String
# Using String multiplication + string slicing
 
# Initializing string
test_str = 'neveropen'
 
# Printing original string
print("The original string is : " + test_str)
 
# Initializing right rot
r_rot = 7
 
# Initializing left rot
l_rot = 3
 
# Rotating string
# using lambda function 
rotate = lambda s: (s * 3)[len(s) + r_rot - l_rot : 2 * len(s) + r_rot - l_rot]
 
# Right and Left Shift characters in String
# Using String multiplication + string slicing
res = rotate(test_str)
 
# Printing result
print("The string after rotation is : " + str(res))


Output

The original string is : neveropen
The string after rotation is : sforLazyroargeek

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:

  1. Initialize an empty list to store the shifted characters.
  2. Use a list comprehension to shift the characters to the right or left based on the rotation value.
  3. Concatenate the shifted characters to form the rotated string.
  4. Return the rotated string.

Python3




# Python3 code to demonstrate working of
# Right and Left Shift characters in String
# Using list comprehension and string concatenation
 
# Initializing string
test_str = 'neveropen'
 
# Printing original string
print("The original string is : " + test_str)
 
# Initializing right rot
r_rot = 7
 
# Initializing left rot
l_rot = 3
 
# Using list comprehension and string concatenation
res = ''.join([test_str[(i + r_rot - l_rot) % len(test_str)]
               for i in range(len(test_str))])
 
# Printing result
print("The string after rotation is : " + res)


Output

The original string is : neveropen
The string after rotation is : sforLazyroargeek

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.

RELATED ARTICLES

Most Popular

Recent Comments