Program to check strings are rotation of each other or not in Python

PythonServer Side ProgrammingProgramming

Suppose we have two English strings s and t, they can be in lowercase and/or uppercase. We have to check whether one is a rotation of the other or not.

So, if the input is like s = "koLKAta" t = "KAtakoL", 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
  • s := s concatenate s
  • return True when t is present in s otherwise False

Example

Let us see the following implementation to get better understanding −

def solve(s, t):
   if len(s) != len(t):
      return False
   s = s + s
   return True if s.find(t) != -1 else False

s = "koLKAta"
t = "KAtakoL"
print(solve(s, t))

Input

"koLKAta", "KAtakoL"

Output

True
raja
Published on 14-Oct-2021 09:40:23
Advertisements