Leetcode 242: Valid Anagram

Checking if two strings are anagrams of each other using a Hash Map.

The original problem is here.

Hash Map Approach

Algorithm

My solution

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        if len(s) != len(t):
            return False
            
        count = {}
        
        # Build the frequency map
        for i in range(len(s)):
            count[s[i]] = count.get(s[i], 0) + 1
            count[t[i]] = count.get(t[i], 0) - 1
            
        # Check if all counts evaluate back to 0
        for val in count.values():
            if val != 0:
                return False
                
        return True

Complexity Analysis