Lessons
Arrays
- Two Sum Problem with Solution
- Best Time to Buy and Sell Stock
- Array Contains Duplicates
- Product of Array Except Self: Optimized Approach
- Maximum Subarray Problem
- Maximum Product Subarray
- Find Minimum in Rotated Sorted Array
- Search in Rotated Sorted Array
- Container With Most Water
- Verifying an Alien Dictionary
- Next Permutation
- Remove Duplicates from Sorted Array
- Find First and Last Position of Element in Sorted Array
- Trapping Rain Water
- Median of Two Sorted Arrays
Dynamic Programming
- Climbing Stairs Problem
- Coin Change Problem
- Longest Increasing Subsequence
- Longest Common Subsequence (LCS)
- Word Break Problem
- Combination Sum Problem
- House Robber Problem
- Decode Ways Problem
- Unique Paths Problem
- Pascal's Triangle Problem
- Generate Parentheses Problem
- Jump Game with Dynamic Programming and Greedy Algorithms
- Regular Expression Matching
- Race Car Problem
Graph
Alien Dictionary
Alien Dictionary problem challenge asks us to determine the order of characters in an alien language, using a list of words already sorted by the language’s custom rules. This guide explores how to derive character precedence using topological sorting, graph dependencies, and string comparison techniques.
We will also answer multiple questions people frequently search for, such as:
- How do I determine the correct order of characters in an alien language?
- What algorithm should I use for character precedence?
- How is a graph constructed from a list of words?
- What is the relationship between topological sorting and lexicographical order?
This article offers the most in-depth explanation available, built entirely from scratch.
What is the Alien Dictionary problem
The Alien Dictionary problem asks you to find the correct character order of an unknown language. You’re given a list of words sorted according to this alien language’s custom alphabet. From this list, your task is to identify a valid character ordering that satisfies the lexicographical order of the input.

The input is a list of strings. The output should be a string representing a valid sequence of the alien language’s characters.
Real-world analogy for understanding
Imagine receiving a list of dictionary words from a completely new language. You don’t know the alphabet, but you know the words are in proper order. By comparing each pair of words, you can gradually deduce how one character comes before another — this is how the alien dictionary works.
For example:
python
1
["hat", "hot", "zap"]
From "hat" and "hot", we can deduce 'a' comes before 'o'. From "hot" and "zap", we can tell 'h' comes before 'z'.
Core concepts behind the Alien Dictionary problem
Solving the Alien Dictionary problem requires a combination of string comparison, graph construction, and topological sorting. Let’s break down each of these.
Character precedence and ordering constraints
To derive the correct character order, we compare adjacent words in the list. The first differing character between two words reveals a direct ordering constraint. That constraint tells us which character comes before another.
For example:
python
1
"abc" and "abd"
Here, the first differing characters are 'c' and 'd'. We conclude: c → d.
Each such relation defines character precedence and helps construct a directed graph.
Graph dependencies in an alien language
Each character becomes a node in a graph. An edge from character A to character B represents that A comes before B in the alien language. The collection of these relationships forms the structure of graph dependencies.
This directed structure captures all known ordering constraints and sets up the next step—determining the final order using topological sorting.
Why a directed acyclic graph
We use a directed acyclic graph (DAG) because:
- Each directed edge represents a one-way precedence between characters.
- There must not be cycles—if a cycle exists, it would mean conflicting constraints, making it impossible to determine a valid character order.
A valid character ordering only exists if the graph is acyclic.
Topological sorting: The key to solving the problem
Once the DAG is built, we must extract a valid linear ordering of the characters. This is where topological sorting comes in. Topological sorting is used to find a sequence of nodes such that for every directed edge U → V, U comes before V in the final ordering.
There are two common methods:
DFS-based topological sort
- Use recursion to traverse each node.
- Keep track of visited nodes and the recursion stack.
- Post-ordering of the DFS gives the correct order.
BFS-based topological sort (Kahn's Algorithm)
- Use a queue to process all nodes with zero incoming edges.
- For each processed node, reduce the in-degree of its neighbors.
- Add new zero in-degree nodes to the queue.
Both approaches work, but BFS is often simpler for beginners.
Step-by-step solution to the Alien Dictionary problem
Let’s now walk through the entire process using a Python implementation.
Step 1: Initialize graph structures
We need:
- An adjacency list to represent outgoing edges.
- An in-degree map to track how many incoming edges each character has.
python
1 2 3 4 5
from collections import defaultdict, deque def alien_order(words): graph = defaultdict(set) in_degree = {char: 0 for word in words for char in word}
Step 2: Derive ordering constraints from word comparisons
We loop through adjacent word pairs and compare them character by character until we find the first mismatch. That mismatch gives a precedence rule.
python
1 2 3 4 5 6 7 8 9 10 11 12 13
for i in range(len(words) - 1): w1, w2 = words[i], words[i + 1] min_length = min(len(w1), len(w2)) if w1.startswith(w2) and len(w1) > len(w2): return "" for j in range(min_length): if w1[j] != w2[j]: if w2[j] not in graph[w1[j]]: graph[w1[j]].add(w2[j]) in_degree[w2[j]] += 1 break
Step 3: Perform topological sorting using BFS
Now we process all characters with zero in-degree and build the result.
python
1 2 3 4 5 6 7 8 9 10 11 12 13
queue = deque([char for char in in_degree if in_degree[char] == 0]) order = [] while queue: current = queue.popleft() order.append(current) for neighbor in graph[current]: in_degree[neighbor] -= 1 if in_degree[neighbor] == 0: queue.append(neighbor) return "".join(order) if len(order) == len(in_degree) else ""
Dry run with example input
Let’s use the following input to demonstrate the process:
python
1 2
words = ["wrt", "wrf", "er", "ett", "rftt"] print(alien_order(words)) # Output: "wertf"
Character comparisons and resulting constraints:
- wrt → wrf → 't' before 'f'
- wrf → er → 'w' before 'e'
- er → ett → 'r' before 't'
Final output via topological sorting: w → e → r → t → f
Time and space complexity
Time Complexity: O(N), where N is the total number of characters and constraints.
Space Complexity: O(C + E), where C is the number of unique characters and E is the number of edges.
Why this problem matters
This problem combines multiple algorithmic concepts:
- It tests understanding of string comparison.
- It builds the ability to create and traverse graphs.
- It introduces topological sorting as a real-world application.
Mastering this pattern helps in tackling a wide range of problems involving dependency resolution, class scheduling, and project planning.
Conclusion
The Alien Dictionary problem is a powerful exercise in analyzing graph dependencies and using topological sorting to resolve character precedence in a custom alphabet. By comparing strings, building a directed acyclic graph, and applying topological techniques, we can uncover valid character orders in any alien language.
This guide has walked through the problem in a way no other article does—methodically, clearly, and in-depth—while answering all related questions and using every critical concept from string comparison to ordering constraints.