Alien Dictionary
Try to solve the Alien Dictionary problem.
We'll cover the following
Statement#
In this challenge, you are given a list of words written in an alien language, where the words are sorted lexicographically by the rules of this language. Surprisingly, the aliens also use English lowercase letters, but possibly in a different order.
Given a list of words written in the alien language, you have to return a string of unique letters sorted in the lexicographical order of the alien language as derived from the list of words.
If there’s no solution, that is, no valid lexicographical ordering, you can return an empty string.
Note: The lexicographic order of a given language is defined by the order in which the letters of its alphabet appear. In English, the letter “n” appears before the letter “r” in the alphabet. As a result, in two words that are the same up to the point where one features “n” and the other features “r,” the former is considered the lexicographically smaller word of the two. For this reason, “ban” is considered lexicographically smaller than “bar.”
Similarly, if an input contains words followed by their prefix, such as “educated” and then “educate,” these cases will never result in a valid alphabet because in a valid alphabet, prefixes are always first.
Constraints:
-
words.length -
words[i].length - All characters in
words[i]are English lowercase letters.
Examples#
1 of 4
2 of 4
3 of 4
4 of 4
Understand the problem#
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps us to check if you’re solving the correct problem:
Alien Dictionary
What is the output if the following list of words is given as input?
Select all that apply.
[“wrt”, “wrf”, “er”, “ett”, “rftt”]
“wrfet”
“wreft”
“wertf”
“wfert”
Figure it out!#
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself#
Implement your solution in main.py in the following coding playground. We have provided some useful code templates in the other file that you may build on to solve this problem.
Solution: Compilation Order
Solution: Alien Dictionary