...

/

Alien Dictionary

Alien Dictionary

Try to solve the Alien Dictionary problem.

Statement

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, 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 "".

If multiple valid orderings exist, you may return any of them.

Note: A string, a, is considered lexicographically smaller than string b if:

  1. At the first position where they differ, the character in a comes before the character in b in the alien alphabet.

  2. If one string is a prefix of the other, the shorter string is considered smaller.

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

canvasAnimation-image
1 / 5

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”] Multi-select

A.

“wrfet”

B.

“wreft”

C.

“wertf”

D.

“wfert”


1 / 4

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.

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

1
2
3
4
5
6

Try it yourself

Implement your solution in the following coding playground:

Java
usercode > Main.java
import java.util.*;
public class Main{
public static String alienOrder(List<String> words) {
// Replace this placeholder return statement with your code
return "";
}
}
Alien Dictionary

Access this course and 1200+ top-rated courses and projects.