Alien Dictionary
Beginner Mode

Problem Statement

There is a new alien language that uses the English alphabet. However, the order among the letters is unknown to you.

You are given a list of strings words from the alien language's dictionary, where the strings in words are sorted lexicographically by the rules of this new language.

Return a string of the unique letters in the new alien language sorted in lexicographically increasing order by the new language's rules. If there is no valid ordering of letters, return "". If there are multiple valid orderings, return any of them.

Additional information

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 100
  • words[i] consists of only lowercase English letters.

Example 1:

Input: words = ["wrt","wrf","er","ett","rftt"]

Output: "wertf"

Explanation: - From "wrt" and "wrf", we can deduce 't' comes before 'f'.

  • From "wrt" and "er", we can deduce 'w' comes before 'e'.
  • From "er" and "ett", we can deduce 'r' comes before 't'.
  • From "ett" and "rftt", we can deduce 'e' comes before 'r'.
    Combining these rules, the order is "wertf".

Example 2:

Input: words = ["z","x"]

Output: "zx"

Example 3:

Input: words = ["z","x","z"]

Output: ""

Explanation: The order is invalid, so return "".

Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →