#24HardGraph & Topological Sort· Kahn's Algorithm with a Min-Heap
Alien Dictionary
Input format
words: array of lowercase strings
Output format
String: the letters in a valid alien order (smallest such string), or `""`
Examples
Input: words = ["wrt", "wrf", "er", "ett", "rftt"]
Output: "wertf"
The pairs give t < f, w < e, r < t and e < r, which force the single order w, e, r, t, f.
Input: words = ["z", "x", "z"]
Output: ""
z < x and x < z form a cycle, so no order exists.
Input: words = ["ab", "adc"]
Output: "abcd"
The only rule is b < d; a and c are unconstrained, so the smallest valid string is abcd.
Constraints
- 1 <= words.length <= 100
- 1 <= words[i].length <= 100
- words[i] consists of lowercase English letters.
Study first
- #graphs
- #topological-sort
- #strings
- #heap
