Gamified challenge · Topological sort
words is a dictionary of lowercase words, already sorted in an alien alphabet that uses the same letters as English in some unknown order. Words are compared like in a normal dictionary: at the first position where they differ, the word with the earlier letter comes first, and a word comes before any longer word it is a prefix of.
Write alienOrder(words) that returns a string containing each letter that appears in the words exactly once, in an order that agrees with the sorted list. Any alphabet that fits passes. If no alphabet can produce this order (the clues contradict each other, or a word comes before its own prefix), return an empty string.
Example: ["wrt", "wrf", "er", "ett", "rftt"] gives the clues t < f, w < e, r < t and e < r, so the answer is "wertf".
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google