Check if strings are rotations of each other or not in Python

PythonServer Side ProgrammingProgramming

Suppose we have two strings s and t, we have to check whether t is a rotation of s or not.

So, if the input is like s = "hello", t = "llohe", then the output will be True.

To solve this, we will follow these steps −

  • if size of s is not same as size of t, then
    • return False
  • temp := s concatenate with s again
  • if count of t in temp > 0, then
    • return True
  • return False

Let us see the following implementation to get better understanding −

Example Code

Live Demo

def solve(s, t):
   if len(s) != len(t):
      return False
 
   temp = s + s
 
   if temp.count(t)> 0:
      return True
   return False

s = "hello"
t = "llohe"
print(solve(s, t))

Input

"hello", "llohe"

Output

True
raja
Published on 15-Jan-2021 10:47:37
Advertisements