Data Structures & Algorithms Master Level Finale
💡
Exercise 160

Alien Dictionary 30 XP Hard

LeetCode Ctrl+Enter Run Ctrl+S Save

Given a sorted dictionary of an alien language, find the order of characters. This is topological sort on character ordering.

Compare adjacent words to find character orderings (edges). Then topological sort. If cycle exists, return empty string.

💡 Pro tip: Understand this problem deeply — don't just memorize the code. Try explaining the approach out loud as if teaching a friend. If you can explain it simply, you truly understand it!

📋 Instructions
Find alien character ordering from ['wrt','wrf','er','ett','rftt']. Print the order.
Compare adjacent words char by char. First difference gives an edge. Then topo sort using all found edges.
🧪 Test Cases
Input
alienOrder(['wrt','wrf','er','ett','rftt'])
Expected
wertf
Test case 1
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.