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
Check if Two Strings are Anagram - Python The task of checking if two strings are anagrams in Python involves determining whether the two strings contain the same characters with the same frequency, but possibly in a different order. For example, given the strings "listen" and "silent", they are anagrams because both contain the same charac
2 min read
Python - How to Check if two lists are reverse equal Sometimes, while working with Python lists, we can have a problem in which we need to check if two lists are reverse of each other. This kind of problem can have application in many domains such as day-day programming and school programming. Let's discuss certain ways in which this task can be perfo
6 min read
Python program to check if given string is pangram The task is to check if a string is a pangram which means it includes every letter of the English alphabet at least once. In this article, weâll look at different ways to check if the string contains all 26 letters.Using Bitmasking Bitmasking uses a number where each bit represents a letter in the a
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
How to check if multiple Strings exist in a list Checking if multiple strings exist in a list is a common task in Python. This can be useful when we are searching for keywords, filtering data or validating inputs. Let's explore various ways to check if multiple strings exist in a list.Using Set Operations (most efficient) The most efficient way to
2 min read
Python - Check if string repeats itself Checking if a string repeats itself means determining whether the string consists of multiple repetitions of a smaller substring. Using slicing and multiplicationWe can check if the string is a repetition of a substring by slicing and reconstructing the string using multiplication.Pythons = "ababab"
3 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 | Checking if starting digits are similar in list Sometimes we may face a problem in which we need to find a list if it contains numbers with the same digits. This particular utility has an application in day-day programming. Let's discuss certain ways in which this task can be achieved. Method #1: Using list comprehension + map() We can approach t
8 min read