Open In App

Python – Check if two strings are Rotationally Equivalent

Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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




# 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

Time Complexity: O(n)
Space Complexity: O(n)

Method #3: Using the inbuilt() function to check if two strings are Rotationally Equivalent

Step-by-step algorithm:

  • Initialize two strings test_str1 and test_str2.
  • Concatenate test_str1 with itself and check if test_str2 is a substring of it.
  • If test_str2 is a substring of the concatenated string, then the strings are rotationally equivalent
  • Print the result.

Python3




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 inbuilt function
res = test_str2 in (test_str1+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

Time complexity: O(n), where n is the length of the concatenated string.
Auxiliary Space: O(n), where n is the length of the concatenated string.

Method #4: Using string concatenation and string search

Step-by-step approach:

  • Initialize the two strings.
  • Concatenate the first string with itself.
  • Check if the second string is a substring of the concatenated string.
  • If the second string is a substring, then the two strings are rotationally equivalent.
  • Print the result.

Python3




# Python3 code to demonstrate working of
# Check if two strings are Rotationally Equivalent
# Using string concatenation and string search
 
# 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 string concatenation and string search
concat_str = test_str1 + test_str1
res = test_str2 in concat_str
 
# 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

Time complexity: O(n), where n is the length of the strings.
Auxiliary space: O(n), where n is the length of the strings



Last Updated : 16 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads