 All Problems
Alien Dictionary
hard
graph
topological sort
bfs
dfs
string
facebook
google
amazon
airbnb

There is a new alien language that uses the English alphabet. However, the order of the letters is different from English. 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 the order they appear in the alien language. If there is no solution, return "". If there are multiple valid solutions, return any.

Example 1:

Input:
4
wrt
wrf
er
ett
rftt
Output: wertf

Example 2:

Input:
3
z
x
Output: zx

Example 3:

Input:
2
z
x
z
Output: (empty — cycle)

Constraints:

  • 1 ≤ words.length ≤ 100
  • 1 ≤ words[i].length ≤ 100
  • words[i] consists of lowercase English letters

Input format: First line: number of words. Then one word per line.

Output format: Ordering string, or empty string if impossible.

Run to check your code against the sample cases, or submit to run every case