Alien Dictionary

Alien Dictionary – Hard Level Problem

Foreign language uses the Latin alphabet, but its letter order differs from the standard English sequence of “a, b, c, … z.

You’re given a list of non-empty words from the dictionary, sorted lexicographically according to the rules of this new language.

Your task is to determine the letter order in this language. If the order is invalid, return an empty string. If there are multiple valid orders, return any one of them.

Alien Dictionary Problem

Examples related to Alien Dictionary Problem

Constraints:

  • Input words will contain characters only from lowercase ‘a’ to ‘z’.
  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 100

Hints to solve Alien Dictionary Problem

Recommended Time & Space Complexity

Aim for a solution with O(N + V + E) time and O(V + E) space, where:

  • N is the total length of all the strings.
  • V is the number of unique characters (vertices).
  • E is the number of edges in the graph.

Hint 1:

  • Think of this problem as a graph task where each character (a to z) is a node. Edges represent dependencies between characters.
  • To determine the edges, try comparing two adjacent words in the given list.

Hint 2:

  • Relative order between characters acts as a directed edge.

  • For example, in [“ape”, “apple”], ‘e’ must come before ‘p’, so there’s an edge e → p.

  • Build an adjacency list by comparing consecutive words and identifying these relationships.

  • Which algorithm can help determine a valid order?

Hint 3:

  • Topological Sort is a good fit for finding valid orderings. Using DFS, traverse the graph built from the adjacency list.

  • Maintain a visited map to track nodes in the current DFS path. A value of False means the node isn’t in the current path, while True indicates it is.

  • Detecting a cycle during traversal means the order is invalid.

Hint 4:

  • Perform a post-order traversal using DFS. When visiting a node and its children without finding a cycle, mark it as False in the visited map and append it to the result list.

  • If a cycle is detected, return an empty string. Otherwise, reverse the result list to get the topological order and return it.

Methods to Solve Alien Dictionary Problem

There are mainly 2 approaches to solve Alien Dictionary problem:

  1. Depth First Search Method
  2. Topological Sort (Kahn’s Algorithm)

1. Depth First Search Method

This approach builds a directed graph from the given words, where edges represent the relative order of characters. DFS is used to traverse the graph, detecting cycles and determining a valid topological order by recording nodes in post-order.

  • Time complexity: O(N+V+E)
  • Space complexity: O(V+E)

Where V is the number of unique characters, E is the number of edges and N is the sum of lengths of all the strings.

Code:

2. Topological Sort (Kahn’s Algorithm)

This method uses an adjacency list and an in-degree array to represent the graph. It processes nodes with zero in-degree using a queue, ensuring a valid topological order. If all nodes are processed, the order is valid; otherwise, a cycle exists.

  • Time complexity: O(N+V+E)
  • Space complexity: O(V+E)

Where V is the number of unique characters, E is the number of edges and N is the sum of lengths of all the strings.

Code:

More Articles