InferenceAI

Enter at least two characters to search.

Start free

Alien Alphabet

hardGraphs40 min50 XP

words is sorted according to an unknown alphabet. Return one valid ordering of all letters that appear. When several letters are available at the same step, choose the smallest in normal English order, so the answer is unique. Return "" if no valid order exists.

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT[["wrt","wrf","er","ett","rftt"]]
TASKalien_alphabet()
EXPECTED OUTPUT"wertf"

Start with the input values shown in this example.

Example 1 of 3

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

Output: "wertf"

Explanation: classic

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

Output: "zx"

Explanation: two letters

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

Output: ""

Explanation: cycle

Hint 1

Compare each adjacent pair of words; the first differing letters give an edge.

Hint 2

A longer word before its own prefix (like 'abc' before 'ab') is invalid.

Hint 3

Topological sort with a min-heap of available letters.

Topics

DSAGraphsPython