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

Input: words = ["tea","ear","art","rat"]
One right answer: "tear"

Example 2

Input: words = ["kiwi","kale","lime"]
One right answer: "eikmwal"

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

Input: words = ["tend","ten"]
One right answer: ""

Constraints

  • 1 ≤ words.length ≤ 10⁵
  • 1 ≤ words[i].length
  • sum of words[i].length ≤ 10⁶
  • words[i] uses only the letters a to z

More Advanced graphs

All Advanced graphs problems