Blind 75
48 / 75Free practice

Alien Dictionary

Hard · Graphs

On LeetCode, Alien Dictionary needs LeetCode Premium (checked October 2026). Here it is free: run and submit in Python, JavaScript, Java or C++ with a free account.

Jump to code

Given nonempty words sorted under an unknown character order, return any ordering of all observed characters consistent with that sort. Return an empty string if no valid ordering exists.

Examples

Example 1

words = ["wrt","wrf","er","ett","rftt"]

Output: "wertf"

Example 2

words = ["z","x"]

Output: "zx"

Constraints

  • The input words will contain characters only from lowercase 'a' to 'z'.
  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 100
Need a hint?

Only the first differing character in adjacent words establishes an ordering edge.

Asked in real interviews

Candidates reported versions of Alien Dictionary from these company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 8 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  [
    "wrt",
    "wrf",
    "er",
    "ett",
    "rftt"
  ]
]
Expected
wertf
Case 2
Input
[
  [
    "z",
    "x"
  ]
]
Expected
zx
Case 3
Input
[
  [
    "z",
    "x",
    "z"
  ]
]
Expected
Case 4
Input
[
  [
    "abc",
    "ab"
  ]
]
Expected
Case 5
Input
[
  [
    "a",
    "b"
  ]
]
Expected
ab

Drafts are saved per language in this browser when storage is available.