Alien Dictionary
Hard Advanced graphs
words is a list of words made of the letters a to z. It is meant to be in dictionary order for an alphabet we cannot see: one that uses our letters, in an order we don't know.
To put two words in dictionary order, look at the first position where their letters differ: the word with the earlier letter there comes first. When one word is the beginning of the other, the shorter one comes first, so a longer word can never be listed before a word it starts with. A word may be listed more than once.
Return an order of the letters that fits the list: a string that holds every letter used in words exactly once, earliest first. Letters the list does not put in order against each other may go in any order, and any order that fits is accepted. If no order fits, return the empty string "".
Example 1
Example 2
kiwi and kale first differ at i and a, and kale and lime at k and l. Only i before a and k before l are fixed, so many orders fit and any is accepted.
Example 3
Constraints
1 ≤ words.length ≤ 10⁵1 ≤ words[i].lengthsum of words[i].length ≤ 10⁶words[i]uses only the lettersatoz
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
More Advanced graphs
- Course ScheduleAI round · Medium
- Cheapest Flights Within K StopsAI round · Medium
- Longest Increasing Path in a MatrixAI round · Hard
- Course Schedule IIMedium
- Evaluate DivisionMedium