A dictionary of words is sorted by the rules of an alien language that uses lowercase English letters. Derive a letter ordering consistent with it. Return the ordering as a string containing every letter that appears in the words exactly once. If several orderings are consistent, return any of them. If no ordering is consistent, because the words imply a cycle or a word is followed by its own proper prefix, return the empty string.
Examples
Example 1:
Input: words = ["wrt","wrf","er","ett","rftt"]
Output: "wertf"
Example 2:
Input: words = ["z","x"]
Output: "zx"
Example 3:
Input: words = ["z","x","z"]
Output: ""
Explanation: z before x and x before z cannot both hold.
Example 4:
Input: words = ["abc","ab"]
Output: ""
Explanation: a word may not be followed by its own proper prefix.
Example 5:
Input: words = ["ca","cb"]
Output: "cab"
Explanation: only a before b is forced, so "abc", "acb" and "cab" are all accepted.
Constraints
1 <= words.length <= 100
1 <= words[i].length <= 20
Words consist of lowercase English letters.
Prerequisites
Graph traversal — DFS and BFS, and a visited set as the thing that makes both terminate. Union-find for connectivity questions; topological order for dependency ones.
How to think about it
1. Neighbors Give the Rules, a Queue Gives the Order Optimal
Intuition
Two neighboring words agree until their first different letter, and that letter decides their order: the first word's letter comes before the second's. So each pair of neighbors gives at most one rule, and nothing after the first difference counts. A word before a longer word that starts with it gives no rule; the other way round, no order can exist. A letter can be placed once every letter that must come before it is placed, so count the rules pointing at each letter, keep the letters with none in a queue, and take from the front. Letters left over are each waiting on another: a cycle. Any order the rules allow is right.
Algorithm
1. Give every letter that appears an empty list of the letters after it and a count of 0. 2. For each pair of neighbors, find the first position where they differ. If there is none and the first word is longer, return the empty string. Otherwise, if the rule is new, add the second word's letter after the first's and add one to its count. 3. Queue every letter whose count is 0. 4. Take the letter at the front and append it to the order; take one off the count of each letter after it, and queue any that reach 0. 5. If every letter is in the order, return it; otherwise the letters left form a cycle: return the empty string.
Time & Space
Time O(C) for C letters in all the words. Space O(1) extra: at most 26 letters and the rules between them.
2. Adjacent Words Give the Edges Optimal
Intuition
Sorted words say almost nothing individually; the information lives BETWEEN neighbours. The first position where two adjacent words differ gives exactly one ordering fact, and nothing after it is implied. The one illegal case is a word followed by its own prefix, which no ordering can justify. Then it is a topological sort. Any valid order is accepted; this one keeps the ready letters in a min-heap, so it always gives the alphabetically first of them, and a plain queue is just as right.
Algorithm
1. Seed every letter that appears, so isolated letters are not lost. 2. For each adjacent pair, find the first differing position and add that edge; equal prefixes with the longer word first are impossible. 3. Topologically sort with a min-heap over zero-in-degree letters. 4. Fewer letters emitted than exist means a cycle: return the empty string.
Time & Space
Time O(total letters + alphabet log alphabet). Space O(alphabet).