# Python – Check if two strings are Rotationally Equivalent

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
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.