VOOZH about

URL: https://www.geeksforgeeks.org/dsa/given-sorted-dictionary-find-precedence-characters/

⇱ Alien Dictionary - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Alien Dictionary

Last Updated : 6 Nov, 2025

Given an array of strings words[], sorted in an alien language. Determine the correct order of letters in this alien language based on the given words. If the order is valid, return a string containing the unique letters in lexicographically increasing order as per the new language's rules.

Note: There can be multiple valid orders for a given input, so you may return any one of them. If no valid order exists due to conflicting constraints, return an empty string.

Examples:

Input: words[] = ["baa", "abcd", "abca", "cab", "cad"]
Output: "bdac"
Explanation:
The pair “baa” and “abcd” suggests ‘b’ appears before ‘a’ in the alien dictionary.
The pair “abcd” and “abca” suggests ‘d’ appears before ‘a’ in the alien dictionary.
The pair “abca” and “cab” suggests ‘a’ appears before ‘c’ in the alien dictionary.
The pair “cab” and “cad” suggests ‘b’ appears before ‘d’ in the alien dictionary.
So, ‘b’→'d’ →‘a’ →‘c’ is a valid ordering.

Input: words[] = ["caa", "aaa", "aab"]
Output: "cab"
Explanation:
The pair "caa" and "aaa" suggests 'c' appears before 'a'.
The pair "aaa" and "aab" suggests 'a' appear before 'b' in the alien dictionary.
So, 'c' → 'a' → 'b' is a valid ordering.

Key Concept:

We are told the words are already sorted according to the alien dictionary — so the key observation is: we can use this ordering to deduce relationships between the letters. Think about how we learn the English alphabet order — we know “cat” comes before “car”, so we infer that 't' comes before 'r'.
Similarly, in the alien language, if we take two consecutive words from the given list and compare them character by character, the first position where they differ gives us a direct clue about letter order.
Now, to find an order that respects all these dependencies, we need to arrange the characters so that no dependency rule is broken.
This is exactly what a topological sort does.

[Approach 1] Using Kahn's algorithm - O(n*m) Time and O(1) Space

For Topological sorting we use Kahn’s Algorithm.
This algorithm helps us process all dependencies between characters in the correct order and also detect if there’s a cycle, which would mean that the given character order is conflicting.

To apply it, we first create a graph of letters by comparing each pair of adjacent words.When we find the first different character between two words — say u from the first and v from the second — we add a directed edge u → v, Once the graph is built, we find all characters whose in-degree is 0 and push them into a queue.
Then, we process the queue one by one for each character removed, we decrease the in-degree of its adjacent letters.Whenever any letter’s in-degree becomes 0, we push it into the queue. By continuing this process, we can build the final order of characters in the alien language.
If, in the end, not all characters are processed, it means there’s a cycle, and hence no valid order exists.


Output
bdac

[Approach 2] Using Depth-First Search - O(n*m) Time and O(1) Space

For topological sorting, we can also use DFS. The idea is similar to what we discussed in the key concept — we compare all adjacent words and create a graph based on the first differing character between them, where an edge u → v means character u appears before v.

After building the graph, we perform a DFS traversal on each unvisited character to determine the correct order. We also use the same idea that we use in cycle detection in a directed graph. If, during DFS, we visit a node that is already in the current recursion stack, it means there’s a cycle, and hence, the character order is conflicting.
During DFS, each character is added to the result after all the characters that depend on it are processed, Because of this, the characters are added in reverse topological order
So, to get the correct order of characters as per the alien language, we reverse the final string.


Output
bdac
Comment