Alien Dictionary
Compare each pair of neighbours and stop at its first differing letter. That one letter gives an arrow. Then run Kahn's algorithm on the arrows: place letters with no arrows pointing in, and remove their arrows as you go.
A letter can be placed next only when every arrow pointing into it has already been placed. Then no rule is broken, because nothing unplaced must come before it.
Problem
You get a list of words. The list is sorted by the dictionary order of an alien language. The alien alphabet uses lowercase letters, but you do not know how those letters are ordered.
Find a letter order that makes the list sorted. Return every letter that appears in the words, once each, in that order, as one string.
More than one order can work. Any valid one is accepted. If no order can make the list sorted, return an empty string.
words: a list of strings, given in the alien dictionary order.
One string with each letter that appears in words exactly once, in a valid alien order. Or an empty string if no order works.
The neighbouring pairs say: b before a (baa vs abcd), d before a (abcd vs abca), a before c (abca vs cab), b before d (cab vs cad). "bdac" respects all four. Any other valid order is also accepted.
caa vs aaa says c comes before a. aaa vs aab says a comes before b. Only one order fits: c, a, b.
ab before ba says a comes before b. ba before ab says b comes before a. Those two rules clash, so no order works.
- • Every word uses only lowercase letters a to z.
- • Some inputs have no valid order. For those, return an empty string.
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
Each pair of neighbouring words tells me something about two letters. Which letters in the pair actually matter?
- 2.
For the words
catandcap, do the letters after the first difference tell me anything? Why or why not? - 3.
If I draw every "x comes before y" fact as an arrow x → y, what kind of graph do I get, and what do I need to find in it?
- 4.
Which letters can go first in the answer?
- 5.
If a set of arrows forms a loop, what happens to the letters in that loop while I keep placing letters that have no arrows pointing in?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Try each order of the letters; keep the first one that sorts the list | O(U! · N · L) | O(U) |
| Optimal | One rule per neighbouring pair; then place letters that have nothing before them | O(N · L) | O(1) |
Why does this belong to this pattern?
- “words sorted in an alien language”The sorted list hides a set of letter comparisons. Build the graph from them, then sort it.
- “return any valid order”This is a topological sort: any order that puts every arrow's tail before its head.
- “empty string if no order”A cycle makes Kahn stop early, so the count of placed letters tells us.
The graph is hidden: the words only say which letter comes first in each neighbouring pair. Each pair gives one rule, from its first differing letter. Letters after that point are never compared for a rule, so the first difference is all you read. The next question, Alien Dictionary, keeps this rule and adds the prefix case and the letters that have no rules.
Don't make this mistake
A string diff marks every mismatch, but a sorted list only decides the pair at its first mismatch. On ["ab", "ca", "cb"], the pair ab vs ca gives a → c at index 0. Reading on gives b vs a at index 1, a false arrow b → a. Together with a → b from ca vs cb, that is a cycle, so the code returns "". But "acb" is a valid answer.
On ["d", "ab", "ba", "ab"], the rules are d → a, a → b and b → a. Kahn places d, then a and b wait on each other. The queue is empty with 1 of 3 letters placed. Returning "d" is wrong: the answer must be empty.
Comparing every pair of words adds rules that the neighbouring pairs already imply, so the answer is still right but the work grows with N². Only neighbours are needed: a sorted list is sorted as soon as each neighbouring pair is in order.
Want to implement this?
Write it yourself on GeeksforGeeks, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Topological Sort translate into production architectures and low-level memory mechanics.