Checking if two strings are anagrams of each other using a Hash Map.
The original problem is here.
s and t do not have the same length, they cannot possibly be anagrams. Return False early.count) to store character frequencies. s, we increment its frequency count in the hash map.t, we decrement its frequency count in the same hash map.0, it means s and t completely balance each other out and are valid anagrams! If any value is non-zero, they are not.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