a tuple of char counts is a hashable dict key
count a word into 26 slots and you have a frequency list. convert that list to a tuple and it becomes hashable — now it can be a dictionary key.
which matters because anagrams always have the same character frequencies → same tuple → same dictionary key:
# "act" and "cat" — a=1, c=1, t=1, everything else 0
count for "act" = [1, 0, 1, 0, ..., 1, 0, 0, 0, 0, 0, 0] → key = (1, 0, 1, 0, ..., 1, 0, 0, 0, 0, 0, 0)
count for "cat" = [1, 0, 1, 0, ..., 1, 0, 0, 0, 0, 0, 0] → key = (1, 0, 1, 0, ..., 1, 0, 0, 0, 0, 0, 0)
↑ same key ↑
so the dictionary groups them for me. i never compare two words to each other:
ans[key] = ["act"]
ans[key].append("cat") # same key, so it joins the same list
# ans[key] == ["act", "cat"]
the part worth keeping: i don't hash anything myself. i just have to hand the dict something hashable, and two different tuple objects with the same values count as the same key — same contents-not-identity rule as comparing two counter dicts.