242. Valid Anagram

# Easy

Three methods:

  1. compare s to t, find the element then delete it in t

  2. use map, traversal s to do increase, then traversal t to do decrease

  3. use sort(), sort them first then compare in order

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        if len(s) != len(t):
            return False
        
        m = {}
        for x in s:
            if x not in m:
                m[x] = 1
            else:
                m[x] += 1
                
        for y in t:
            if (y not in m) or m[y] == 0:
                return False
            m[y] -= 1
            
        return True

Last updated