Meta Data#
Difficulty: easy First Attempt: 2022-08-22
- Submissions: Accepted ×3 → Runtime Error ×2 → Accepted → Runtime Error → Wrong Answer → Accepted → Wrong Answer ×4
Approach#
Anagrams have identical character counts (Hash Table Template 1). Counter(s) == Counter(t) does it in one line; by hand, one dict gets +1 for each character of s and -1 for each of t, and every value must end at 0.
My Attempts#
In 2022-08 both sorted and Counter passed; then I tried counting by hand:
tracker.get(x, 0) + 1never stored the result, so the map stayed empty andall(...)on nothing isTrue:"a"and"c"returnedTrue.
A 2022-11 rewrite failed four more times:
- The second loop decremented
alpha_count[s], whereswas the leftover variable from the first loop rather than the current charactert, and only the sum was checked, letting positives and negatives cancel:"ac"and"aa"returnedTrue. - The last version used
not all(values), meaning “some count is 0”:"ba"and"b"returnedTrue.
Solution#
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
cnt = defaultdict(int)
for a, b in zip(s, t):
cnt[a] += 1
cnt[b] -= 1
return all(v == 0 for v in cnt.values())
Complexity#
Time O(n), space O(k) for k distinct characters.
Mistakes to Avoid#
- Check that every count is 0, not their sum:
+1and-1cancel out. tracker.get(x, 0) + 1computes a value but never stores it.
Interview Explanation#
One hash map adds one for each character of the first string and subtracts one for the second; if every count ends at zero, they are anagrams. Time O(n); with lowercase letters only, a size-26 array works.
