Alien Dictionary
Infer a valid character order from words sorted in an unknown alphabet.
Why does this pattern fit?
Restate the exact job
Infer a valid character order from words sorted in an unknown alphabet.
Only the first differing character between adjacent words creates an ordering edge.
O(total characters + alphabet edges) time
Adding edges from every differing position invents false constraints.
How to solve Alien Dictionary
The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.
What the question asks
Infer a valid character order from words sorted in an unknown alphabet.
Why Graphs: traversal & ordering fits
Only the first differing character between adjacent words creates an ordering edge.
State to maintain
A graph of characters and indegrees.
Transition
Build edges, reject invalid prefix order, then topologically sort all characters.
Time and space
O(total characters + alphabet edges) time
Counterexample to the tempting mistake
Adding edges from every differing position invents false constraints.
Prove it again tomorrow
Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.