Alien Dictionary

Try to solve the Alien Dictionary problem.

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:

  • 11 \leq words.length 103\leq 10^3
  • 11 \leq words[i].length 20\leq 20
  • All characters in words[i] are English lowercase letters.

Examples#

Created with Fabric.js 3.6.6 Output Sample example 1 Input words "ba" "ba" "ba" order "ab"

1 of 4

Created with Fabric.js 3.6.6 Output Sample example 2 Input words "hello" "world" "world" order "helordw"

2 of 4

Created with Fabric.js 3.6.6 Output Sample example 3 Input order "artevxhmplo" words "xro" "xma" "per" "pert" "oxh" "olv"

3 of 4

Created with Fabric.js 3.6.6 Output Sample example 4 Input order "" words "mdx" "mars" "avgd" "dkae"

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

1

What is the output if the following list of words is given as input?

Select all that apply.

[“wrt”, “wrf”, “er”, “ett”, “rftt”]

A)

“wrfet”

B)

“wreft”

C)

“wertf”

D)

“wfert”

Question 1 of 40 attempted

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.

Drag and drop the cards to rearrange them in the correct sequence.

Build the graph from the input using adjacency lists.

Remove the sources, i.e. vertices with indegree =0= 0 from the graph and add them to a results array.

Decrement the indegree of the sources children by 1.

Repeat until all nodes are visited.


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.

Python
main.py
graph.py
Input #1
%0 node_01 xro node_11 xma node_21 per node_31 prt node_41 oxh node_51 olv
Visualization for Input #1
Alien Dictionary

Solution: Compilation Order

Solution: Alien Dictionary