Skip to content
AI360Xpert
Beta
Difficulty: HardAdvanced Graphs

Alien Dictionary

Problem in Plain English

Sorted alien words reveal which letter precedes which other letter. Rebuild the full letter order from those clues, or return an empty string when the clues contradict each other.

Problem Statement

You are given a list of strings words from an alien dictionary, sorted lexicographically by the rules of that language. Return each observed letter exactly once in alphabet order. Any valid order is accepted when multiple orders are possible.

Lexicographic comparison uses the first differing characters; if one word is a prefix of another, the shorter word comes first. Return an empty string when the clues contradict each other or a longer word wrongly precedes its own prefix.

Constraints

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 100
  • words[i] holds only lowercase English letters.

Examples

Example 1

Input
words = ["wrt","wrf","er","ett","rftt"]
Output
"wertf"

Adjacent pairs give t before f, w before e, r before t, and e before r, chaining into w to e to r to t to f.

Example 2

Input
words = ["z","x"]
Output
"zx"

The single differing pair puts z before x.

Example 3

Input
words = ["z","x","z"]
Output
""

The clues demand z before x and x before z, which is a cycle, so no order exists.

Example 4

Input
words = ["abc","ab"]
Output
""

No alphabet can put a longer word before its exact prefix. Reject this even though no character differs and no edge is added.

Example 5

Input
words = ["aba"]
Output
"ab" (one valid order)

A single word imposes no precedence rules. Both ab and ba are valid, but each observed letter appears only once despite the repeated a.

Intuition

Compare adjacent words at their first mismatch: wrt and wrf say t must precede f, regardless of later characters. Represent that clue as the directed edge t -> f. Example 1 also reveals w -> e, r -> t and e -> r, making w -> e -> r -> t -> f. An alphabet satisfying every edge is a topological order. Include letters with no edges too, reject a longer word before its own prefix, and detect cycles because no letter in a cycle can come before all the others.

Approaches

DFS Postorder Topo Sort

Solution Details

Reveal DFS Postorder Topo Sort: intuition, complexity, and code

Hints

Hint 1
Only the first differing letters of neighbouring words reveal an order rule, so compare each adjacent pair once.
Hint 2
Those rules form a directed graph over the letters, which calls for an ordering algorithm rather than sorting.
Hint 3
Count incoming rules per letter, emit letters with none left, and report failure if any letter never becomes free.

Edge Cases

  • A longer word placed before its own prefix, such as ["abc","ab"], which is invalid on sight and returns "".
  • Cyclic rules such as ["z","x","z"], where no letter ever runs out of predecessors.
  • Letters that never appear in any rule, which start free and may sit anywhere valid in the answer.
  • Identical words, such as ["aa","aa"], add no edge; return the observed letter a once.
  • Repeated clues, such as ["za","zb","ca","cb"], include a -> b twice but count it only once.
  • A valid prefix pair such as ["ab","abc"] adds no edge; all three letters still belong in the answer.

Common Mistakes and Interview Tips

  • Missing the longer-before-prefix check, which wrongly builds rules for a dictionary that is already invalid.
  • Adding a duplicate rule twice and double-counting indegrees, which strands a letter that should have been freed.
  • Omitting rule-free letters from the graph, which drops them from the returned alphabet.
  • Adding an edge for every mismatch: ["ab","ba"] constrains only a -> b; adding b -> a invents a cycle.
  • Treating a completed DFS letter as active reports false cycles; distinguish unvisited, active and finished states.
  • Interview tip: explain the prefix rejection separately from cycle detection, and accept any valid alphabet rather than requiring English alphabetical order.

Key Takeaway

Pairwise precedence clues become directed edges, and peeling off predecessor-free nodes yields the global order or exposes the contradiction.