DEV Community

Mubashir
Mubashir

Posted on

Valid Anagram

OVERVIEW
In this task, I worked on checking whether two strings are anagrams of each other.
It means that both strings contain the same characters with the same frequency, just arranged differently.

MY APPROACH
I created a function that takes two strings s and t, and returns True if they are anagrams, otherwise False.

EXAMPLE
INPUT : s = "anagram"
t = "nagaram"
OUTPUT : TRUE

LOGIC IMPLEMENTED

  • Sort both strings
  • Compare them If both sorted strings are equal, they are anagrams.

HOW THE PROGRAM WORKS

  • Sorting arranges characters in order
  • If both strings have same characters, sorted results will match
class Solution:
    def isAnagram(self, s, t):
        return sorted(s) == sorted(t)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)