Skip to main content
All Questions

Alien Dictionary

Hard
Unlock detailed company stats for this questionUpgrade

An alien language uses lowercase English letters, but in an unknown order. You're given a list of words from its dictionary, sorted in that language's alphabetical order.

Return a string of the unique letters in words, arranged in an order consistent with how the words are sorted. If no order of letters could produce this sorting, return "". If several orders work, you can return any of them.

Words are compared the usual way: by the first position where they differ, and a word comes before any longer word that starts with it.

Example 1

Input: words = ["bca", "aaa", "acb"] Output: "bac"

bca comes before aaa, so b comes before a. aaa comes before acb, so a comes before c.

Example 2

Input: words = ["abc", "ab"] Output: ""

ab is a prefix of abc, so it has to come first under any order of letters.

Constraints

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

Related questions

Flatten a Dictionary

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.