PROBLEM 46 OF 75

Alien Dictionary

Infer a valid character order from words sorted in an unknown alphabet.

PATTERNGraphs: traversal & orderingNodes are facts; edges are allowed moves.
SPOT THE SIGNAL

Why does this pattern fit?

FRAME · 1 OF 4

Restate the exact job

Infer a valid character order from words sorted in an unknown alphabet.

WHY THIS FITS

Only the first differing character between adjacent words creates an ordering edge.

COMPLEXITY

O(total characters + alphabet edges) time

COMMON FAILURE

Adding edges from every differing position invents false constraints.

THE REASONING, IN ONE PLACE

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.

Open blank recall ↗