This question evaluates the ability to infer ordering constraints among characters, model dependencies from ordered examples, and reason about edge cases in lexicographic sequences. Commonly asked in Coding & Algorithms interviews, it assesses both practical algorithmic implementation and conceptual understanding within the domains of string processing and graph-based ordering problems.
You are given a list of words sorted in lexicographic order according to an unknown ("alien") alphabet.
Return one valid ordering of the unique characters in this alien alphabet that is consistent with the given sorted word list. If no valid ordering exists, return an empty string.
words
is already sorted according to the alien alphabet.
words
: an array of strings, length
n
.
""
if impossible.
1 <= n <= 10^4
1 <= len(words[i]) <= 100
word1
is a strict prefix of
word2
, then
word1
must appear before
word2
in the sorted list (otherwise the ordering is invalid).
Input: words = ["wrt", "wrf", "er", "ett", "rftt"]
Possible output: "wertf"