DSAHashingSignature key

Group Anagrams Visualizer

What is Group Anagrams

Anagrams share a letter multiset. Sort the letters of a word (or pack a 26-count tuple) and use that string as a hash-map key. Every word with the same key falls into the same bucket.

  • Key = sorted word: eat, tea, ate all become “aet”.
  • One pass: append to the bucket, never compare every pair of words.
  • Judge-stable print: sort inside each group, then sort the groups.

How this visualizer works

Words eat tea tan ate nat bat. Watch buckets aet, ant, and abt fill.

Interactive trace

hash_table.state

Input
Hash table
Current look-up
Match / window
Stored entry

current_action

Load an input, then press Play or Step.

live_trace.py

1def groupAnagrams(words):
2groups = {}
3for w in words:
4key = ''.join(sorted(w))
5groups.setdefault(key, []).append(w)
6return list(groups.values())

stdout — decision log

// each hash probe is recorded here
Speed Ready

Pseudocode & Complexity

idea.txt

1def groupAnagrams(words):
2groups = {}
3for w in words:
4key = ''.join(sorted(w))
5groups.setdefault(key, []).append(w)
6return list(groups.values())

complexity.md

OperationTime
Key of one wordO(L log L)
All wordsO(n L log L)
SpaceO(n L)

Python implementation

solution.py

1def groupAnagrams(words):
2 groups = {}
3 for w in words:
4 key = ''.join(sorted(w))
5 groups.setdefault(key, []).append(w)
6 return list(groups.values())