Recover an alien alphabet from a sorted word list with topological sort
Problem
A dictionary written in an alien language is sorted by the alien alphabet, which uses ordinary characters in an unknown order. Given the dictionary's words in sorted order, work out the alphabet. Write alienOrder(words), which returns a string containing every distinct character that appears in the words, in an order consistent with the dictionary.
alienOrder(["wrt", "wrf", "er", "ett", "rftt"]); // "wertf"
alienOrder(["z", "x"]); // "zx"
alienOrder(["z", "x", "z"]); // "" (z before x before z is impossible)Ref. UR-1421 · © UIRound · uiround.com/questions/recover-an-alien-alphabet-from-a-sorted-word-list-with-topological-sort
Unlock this question
The full problem, its tests and the tested solution are part of Pro.
- 2
- code listings
- 5
- edge cases
What's inside
- The full problem, with examples
- Tests that check your solution
- Step-by-step
- Approach
- Solution
- Complexity
- Edge cases
- Follow-up questions
- Card worldwide
- UPI and net banking in India
- One-time payment, nothing renews
Stuck? Take a hint
- 1Compare each word only with the next one. The first position where they differ tells you one letter comes before another; nothing after that position tells you anything.
Try it in the editor
Run your code as often as you like. Submitting it to the full test suite is part of Pro.
Private, and saved in this browser as you type. All your notes are on My lists.