Valid Anagram

Given two strings s and t, write a function to determine if t is an anagram of s. Tags: String

Try It!

Discussion

Video

Solution

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