Tuesday, November 26, 2024
Google search engine
HomeLanguagesPython3 Program for Left Rotation and Right Rotation of a String

Python3 Program for Left Rotation and Right Rotation of a String

Given a string of size n, write functions to perform the following operations on a string-

  1. Left (Or anticlockwise) rotate the given string by d elements (where d <= n)
  2. Right (Or clockwise) rotate the given string by d elements (where d <= n).

Examples: 

Input : s = "neveropen"
        d = 2
Output : Left Rotation  : "eksforGeeksGe" 
         Right Rotation : "ksGeeksforGee"  


Input : s = "qwertyu" 
        d = 2
Output : Left rotation : "ertyuqw"
         Right rotation : "yuqwert"

Method 1:

A Simple Solution is to use a temporary string to do rotations. For left rotation, first, copy last n-d characters, then copy first d characters in order to the temporary string. For right rotation, first, copy last d characters, then copy n-d characters. 

Can we do both rotations in-place and O(n) time? 
The idea is based on a reversal algorithm for rotation.

// Left rotate string s by d (Assuming d <= n)
leftRotate(s, d)
  reverse(s, 0, d-1); // Reverse substring s[0..d-1]
  reverse(s, d, n-1); // Reverse substring s[d..n-1]
  reverse(s, 0, n-1); // Reverse whole string.  

// Right rotate string s by d (Assuming d <= n)
rightRotate(s, d)

  // We can also call above reverse steps
  // with d = n-d.
  leftRotate(s, n-d)  

Below is the implementation of the above steps : 

Python3




# Python3 program for Left
# Rotation and Right
# Rotation of a String
 
# In-place rotates s towards left by d
def leftrotate(s, d):
    tmp = s[d : ] + s[0 : d]
    return tmp
   
# In-place rotates s
# towards right by d
def rightrotate(s, d):
   
   return leftrotate(s, len(s) - d)
 
# Driver code
if __name__=="__main__":
     
    str1 = "neveropen"
    print(leftrotate(str1, 2))
  
    str2 = "neveropen"
    print(rightrotate(str2, 2))
 
# This code is contributed by Rutvik_56


Output: 

Left rotation:  eksforGeeksGe
Right rotation:  ksGeeksforGee                 

Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time 
Auxiliary Space: O(1), as we are not using any extra space.

Method 2:

We can use extended string which is double in size of normal string to rotate string. For left rotation, access the extended string from index n to the index len(string) + n. For right rotation, rotate the string left with size-d places.

Approach:

The approach is

// Left rotate string s by d 
leftRotate(s, n)
temp = s + s; // extended string 
l1  = s.length // length of string 
return temp[n : l1+n] //return rotated string.

// Right rotate string s by n
rightRotate(s, n)
// We can also call above reverse steps
// with x = s.length - n.
leftRotate(s, x-n)

Below is implementation of above approach:

Python3




# Python3 program for Left
# Rotation and Right
# Rotation of a String
  
def leftrotate(str1, n):
    # extended string
    temp = str1 + str1
    l = len(str1)
    # Return string
    return temp[n :l+n]
def rightrotate(str1, n):
    return leftrotate(str1, len(str1)-n)
      
    return temp[l-n : l1-n  ]
# Driver code
if __name__=="__main__":
      
    str1 = "neveropen"
    print(leftrotate(str1, 2))
  
    str2 = "neveropen"
    print(rightrotate(str2, 2))
  
# This code is contributed by Susobhan Akhuli


Output

eksforGeeksGe
ksGeeksforGee

Time Complexity: O(N), where N is the size of the given string.
Auxiliary Space: O(N)

Please refer complete article on Left Rotation and Right Rotation of a String for more details!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
You’ll access excellent video content by our CEO, Sandeep Jain, tackle common interview questions, and engage in real-time coding contests covering various DSA topics. We’re here to prepare you thoroughly for online assessments and interviews.
Ready to dive in? Explore our free demo content and join our DSA course, trusted by over 100,000neveropen! Whether it’s DSA in C++, Java, Python, or JavaScript we’ve got you covered. Let’s embark on this exciting journey together!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments