Alien Dictionary
HardP3Coding
An alien language uses the lower-case English letters in an unknown order. You are given words, a list that is sorted lexicographically according to that unknown alphabet.
Return a string of the distinct letters that appear in words, ordered consistently with the list. Several orders can be valid, so return the lexicographically smallest valid one in the normal English alphabet (when more than one letter could come next, take the smallest). If the list is inconsistent (a cycle in the implied order, or a longer word placed before its own prefix), return "".
Example 1
- Input
- words = ["wrt","wrf","er","ett","rftt"]
- Output
- "wertf"
- Why
- t < f, w < e, r < t, e < r gives w < e < r < t < f.
Example 2
- Input
- words = ["z","x","z"]
- Output
- ""
- Why
- z < x and x < z cannot both hold.
Constraints
- 1 <= words.length <= 100
- 1 <= words[i].length <= 100
- words[i] consists of lower-case letters
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 10. ⌘Enter runs, ⌘⇧Enter submits.