Problem. An alien language uses lowercase English letters in an unknown order. You get a list of words sorted
lexicographically in that order. Return a string of all the letters that appear, in an order consistent with the list, or ""
if no such order exists.
words = ["wrt", "wrf", "er", "ett", "rftt"] → "wertf"
words = ["z", "x", "z"] → "" (z < x and x < z)
words = ["abc", "ab"] → "" (a longer word can't come before its own prefix)
Sorted order hides constraints: comparing two adjacent words, the first position where they differ says which letter comes first (from "wrt" before "wrf": t < f). Collect those constraints as edges, and a consistent alphabet is a topological order (lesson 11.7) of the letters.
ML context. Inferring an order from pairwise comparisons is a ranking problem: annotators say "response A is better than B", the results of matches say who beat whom, and you want a total order consistent with them. When the comparisons are consistent, it's a topological sort; when they contradict (a cycle), the contradiction must be reported, which is exactly the "" case here (Change D).