Check whether two strings are anagram of each other in Python

PythonServer Side ProgrammingProgramming

Suppose we have two strings s and t we have to check whether they are anagram of each other or not.

So, if the input is like s = "bite" t = "biet", then the output will be True as s ad t are made of same characters.

To solve this, we will follow these steps −

  • if size of s is not same as size of t, then
    • return False
  • sort characters of s and t
  • return true if s is exactly same as t, otherwise 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
   s = sorted(s)
   t = sorted(t)
   return s == t

s = "bite"
t = "biet"
print(solve(s, t))


"bite", "biet"


Published on 16-Jan-2021 09:38:38