2Brute2Best
🔥0

Alien Dictionary

Hard·LeetCode #269 Premium Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You get a list of words. The words are in the dictionary order of an alien language. The alien alphabet uses lowercase letters, but their order is unknown.

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.

Any valid order is accepted. If no order can make the list sorted, return an empty string. That happens when two rules clash, or when a longer word comes before its own prefix.

Input

words: a list of strings in the alien dictionary order (the order may be impossible).

Output

One string with each letter that appears in words once, in a valid alien order. Or an empty string if none exists.

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

wrt vs wrf gives t before f. wrf vs er gives w before e. er vs ett gives r before t. ett vs rftt gives e before r. Those rules form one chain: w, e, r, t, f.

Example 2
Input: words = ["z", "x"]
Output: "zx"

z vs x says z comes before x. That is the only order.

Example 3
Input: words = ["z", "x", "z"]
Output: ""

z before x, then x before z. Those two rules form a cycle, so no order works.

Constraints
  • • Every word uses only lowercase letters a to z.
  • • Some inputs have no valid order. For those, return an empty string.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“words sorted in an alien language”
→ The sorted list hides letter comparisons. Build the graph from neighbouring pairs, then sort it.
“return any valid order”
→ A topological sort: any order that puts every arrow's tail before its head.
“empty string when impossible”
→ Two sources of impossibility: a cycle (Kahn stops early) and a longer word before its prefix (a check on the pair).
03

Approach Complexity Ladder

Brute Force — try every letter orderO(U! · N · L)O(U)

Try each order of the letters; keep the first one that keeps the list sorted

Optimal — first-difference rules + KahnO(N · L)O(1)

Rule per pair from its first difference; prefix check; Kahn over letters that appear

Brute Force Intuition

The answer uses each letter that appears exactly once, so it is one order of those letters. Try every order. For each one, check that every word is at most the word after it, comparing letters by their place in that order. A shorter word that matches the start of a longer one counts as smaller. The first order that passes is the answer.

Recognition rule for this sub-pattern:

The graph is hidden — derive edges from comparisons (adjacent words, sequences), then topo-sort.
Remember this · Topological Sort
Trigger
Prerequisites, dependencies, build order, "can all courses be finished?", ordering letters from sorted words.
Think
Is this a directed graph where some things must happen before others?
Key question
Which nodes currently have in-degree 0?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Topological Sort translate into production architectures and low-level memory mechanics.