Valid Anagram (leetcode 242)
✔️Valid Anagram (leetcode 242)
#LeetCode
Pattern: Arrays
Idea: Individual count of each char and total length should match
💻 Code
from collections import defaultdict
def isAnagram( s: str, t: str) -> bool:
total_cnt = 0
chars = defaultdict(int)
for c in s:
chars[c] += 1
total_cnt += 1
for c in t:
if chars[c] == 0:
return False
else:
chars[c] -=1
total_cnt -= 1
if total_cnt == 0:
return True
else:
return False
🔗References
🏢 Companies
Amazon, Google, Microsoft, Facebook, Apple, Bloomberg, Goldman Sachs, Spotify, Affirm