#418 · Graphs · Advanced · 160 XP · about 35 min
Explorers found a dictionary of a language that uses the lowercase letters a to z, but in a different alphabetical order. The words in words are sorted by that order, the way a dictionary is: two words are compared at the first position where they differ, and a word comes before every longer word that starts with it.
Write alien_order(words) that returns the alien alphabet: a string holding every letter that appears in the words, each once, in an order under which words is sorted.
words sorted, return ''.alien_order(['tea', 'ten', 'ant', 'nat', 'nap']) # returns 'etanp' alien_order(['cab', 'cba', 'bc']) # returns 'acb' alien_order(['abc', 'ab']) # returns '' alien_order(['x', 'y', 'x']) # returns ''
In the first example the words show that a comes before n (from 'tea' and 'ten'), t before a, and t before p. Nothing in them says where e goes, so it takes the earliest place it can. In the third, a longer word comes before its own start, and in the last, x would have to come both before and after y.
First solves only, ranked by stars and then time.