Alien Dictionary
Detailed guide and Python implementation for the 'Alien Dictionary' problem.
Problem Statement
There is a new alien language that uses the English alphabet. However, the order of 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 according to 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 solution, return "". If there are multiple solutions, return any of them.
Write a function alienOrder(words: List[str]) -> str.
- •1 <= len(words) <= 100
- •1 <= len(words[i]) <= 100
- •words[i] consists of lowercase English letters
Examples
words = ["wrt","wrf","er","ett","rftt"]
"wertf"
From the sorted words, we can derive the relationships: 'w' -> 'e', 'r' -> 't', 't' -> 'f', etc.
words = ["z","x"]
"zx"
From "z" and "x", we know 'z' comes before 'x'.
words = ["z","x","z"]
""
The order is invalid because 'z' comes before 'x' and 'x' comes before 'z'.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
How to Iterate Through a Dictionary in Python
Learn how to loop or iterate through a Python dictionary. Discover methods to loop over keys, values, and key-value pairs with clean, runnable examples.
Python Dictionary Methods
Learn Python dictionary methods. Complete reference guide for key-value pair insertions, retrievals, updates, and checks.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.