An Anagram I Am in Python

PythonServer Side ProgrammingProgramming

Suppose we have two strings s0 and s1, we have to check whether they are anagrams of each other or not. As we know two strings are said to be anagrams when we can rearrange one to become the other.

So, if the input is like s0 = "listen", s1 = "silent", then the output will be True.

To solve this, we will follow these steps −

  • sort the characters of s0 and s1

  • if sorted sequence of characters of s0 and s1 are same, then

    • return True

  • otherwise return False

Let us see the following implementation to get better understanding −


 Live Demo

class Solution:
   def solve(self, s0, s1):
      return sorted(s0) == sorted(s1)
ob = Solution()
print(ob.solve("listen", "silent"))


"listen", "silent"


Updated on 02-Sep-2020 12:49:44