Check if a string is a rotation of another string in Python

Problem

Write a Python function that checks if two strings are rotation of each other

Solution

Rotating a string means inserting a number of characters from the end of the string to the beginning of the string. Any easy trick to check if two strings are rotation of each other is to concatenate one string to itself and then check if the other string is a substring in the concatenation

Example

Take a look at the following example

Note that Hello is a substring in lloHelloHe

Code

Here is the code in Python

If you run the test cases, you should get something like…

Thanks for visiting. For questions, please use the comments section below

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *