Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

Alien Dictionary

Hard

Topics
GraphTopological SortBFS

There is a new alien language that uses the English alphabet. However, the order among letters is unknown. You are given a list of strings words from the alien language dictionary, where the strings 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 no valid order exists, return "". If there are multiple valid orderings, return any of them.

Example 1

Input:  words = ["wrt","wrf","er","ett","rftt"]
Output: "wertf"
Explanation: From "wrt" and "wrf": t < f. From "wrt" and "er": w < e. From "er" and "ett": r < t. From "ett" and "rftt": e < r.

Example 2

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

Example 3

Input:  words = ["z","x","z"]
Output: ""
Explanation: Invalid ordering — cycle detected.

Constraints

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 100
  • words[i] consists of only lowercase English letters.
Run ⌘' · Submit ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access