Skip to content
Related Articles

Related Articles

Improve Article

Python – Check if two strings are Rotationally Equivalent

  • Last Updated : 03 Jul, 2020

Sometimes, while working with Python Strings, we can have problem in which we need to check if one string can be derived from other upon left or right rotation. This kind of problem can have application in many domains such as web development and competitive programming. Let’s discuss certain ways in which this task can be performed.

Input : test_str1 = ‘GFG’, test_str2 = ‘FGG’
Output : True

Input : test_str1 = ‘geeks’, test_str2 = ‘ksege’
Output : False

Method #1 : Using loop + string slicing
The combination of above functions can be used to solve this problem. In this, we perform the task of extracting strings for performing all possible rotations, to check if any rotation equals the other string.




# Python3 code to demonstrate working of 
# Check if two strings are Rotationally Equivalent
# Using loop + string slicing
  
# initializing strings
test_str1 = 'geeks'
test_str2 = 'eksge'
  
# printing original strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
  
# Check if two strings are Rotationally Equivalent
# Using loop + string slicing
res = False
for idx in range(len(test_str1)):
        if test_str1[idx: ] + test_str1[ :idx] == test_str2:
            res = True
            break
  
# printing result 
print("Are two strings Rotationally equal ? : " + str(res)) 
Output :



The original string 1 is : geeks
The original string 2 is : eksge
Are two strings Rotationally equal ? : True

 

Method #2 : Using any() + join() + enumerate()
This is one of the ways in which this task can be performed. In this, we perform the task of checking any rotational equivalent using any() extracted using nested generator expression and enumerate().




# Python3 code to demonstrate working of 
# Check if two strings are Rotationally Equivalent
# Using any() + join() + enumerate()
  
# initializing strings
test_str1 = 'geeks'
test_str2 = 'eksge'
  
# printing original strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
  
# Check if two strings are Rotationally Equivalent
# Using any() + join() + enumerate()
res = any(''.join([test_str2[idx2 - idx1] 
        for idx2, val2 in enumerate(test_str2)]) == test_str1
        for idx1, val1 in enumerate(test_str1))
  
# printing result 
print("Are two strings Rotationally equal ? : " + str(res)) 
Output :
The original string 1 is : geeks
The original string 2 is : eksge
Are two strings Rotationally equal ? : True

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :