Skip to content

Alien Dictionary

HardGraphs
Decode it first. Clues, constraints, pattern and plan before any code.

An alien language uses lowercase English letters in an unknown order. You get a list of its words, sorted in the alien alphabet's order. Return a string of every letter that appears, arranged in an order consistent with that sorting. If no order works (the list is contradictory or impossible), return "".

Words compare like a dictionary: the first position where two words differ decides, and if one word is a prefix of the other, the shorter word comes first. If more than one order fits, any of them is accepted; the tests here only use lists where exactly one order fits.

words = ["wrt", "wrf", "er", "ett", "rftt"]  →  "wertf"
words = ["z", "x", "z"]                       →  ""     (z before x and x before z)
words = ["abc", "ab"]                         →  ""     (a longer word cannot come before its prefix)

Constraints

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