Python - Check if two strings are Rotationally Equivalent
Last Updated :
16 May, 2023
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))
OutputThe 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))
OutputThe 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
Similar Reads
Python - Check if Splits are equal Given String separated by delim, check if all splits are similar. Input : '45#45#45', delim = '#' Output : True Explanation : All equal to 45. Input : '4@5@5', delim = '@' Output : False Explanation : 1st segment equal to 4, rest 5. Method #1 : Using set() + len() + split() In this, we perform split
2 min read
Python3 Program to Check if strings are rotations of each other or not | Set 2 Given two strings s1 and s2, check whether s2 is a rotation of s1. Examples: Input : ABACD, CDABAOutput : TrueInput : GEEKS, EKSGEOutput : TrueWe have discussed an approach in earlier post which handles substring match as a pattern. In this post, we will be going to use KMP algorithm's lps (longest
2 min read
Python | Check if two list of tuples are identical Sometimes, while working with tuples, we can have a problem in which we have list of tuples and we need to test if they are exactly identical. This is a very basic problem and can occur in any domain. Let's discuss certain ways in which this task can be done. Method #1 : Using == operator This is th
4 min read
Python Program To Check If A String Is Substring Of Another Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples :Â Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output
3 min read
Python | Ways to check string contain all same characters Given a list of strings, write a Python program to check whether each string has all the characters same or not. Given below are a few methods to check the same. Method #1: Using Naive Method [Inefficient] Python3 # Python code to demonstrate # to check whether string contains # all characters same
5 min read