Tutorialspoint
Problem
Solution
Submissions

Alien Dictionary Problem

Certification: Advanced Level Accuracy: 0% Submissions: 0 Points: 15

Write a C# program to implement the FindOrder(string[] words) function. You are given a list of words in an alien language where the order of letters is unknown. The words are sorted lexicographically according to this alien language. Derive the order of letters in this language.

Example 1
  • Input: words = ["wrt", "wrf", "er", "ett", "rftt"]
  • Output: "wertf"
  • Explanation:
    • Step 1: Compare adjacent words to find character relationships.
      • From "wrt" and "wrf", we know 't' comes before 'f'.
      • From "wrt" and "er", we know 'w' comes before 'e'.
      • From "er" and "ett", we know 'r' comes before 't'.
      • From "ett" and "rftt", we know 'e' comes before 'r'.
    • Step 2: Build a directed graph from these character relationships.
    • Step 3: Perform a topological sort on the graph.
    • Step 4: Return the sorted order "wertf".
Example 2
  • Input: words = ["z", "x"]
  • Output: "zx"
  • Explanation:
    • Step 1: Compare adjacent words to find character relationships.
      • From "z" and "x", we know 'z' comes before 'x'.
    • Step 2: Build a directed graph from these character relationships.
    • Step 3: Perform a topological sort on the graph.
    • Step 4: Return the sorted order "zx".
Constraints
  • 1 ≤ words.length ≤ 100
  • 1 ≤ words[i].length ≤ 100
  • words[i] consists of only lowercase English letters
  • Time Complexity: O(C) where C is the total length of all words in the input list
  • Space Complexity: O(1) or O(U + min(U², N)), where U is the total number of unique letters and N is the total length of all words
GraphAlgorithmsGoldman SachsD. E. Shaw
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Build a graph where each node is a letter and each edge represents the order between two letters
  • Compare adjacent words to determine the relative order of characters
  • Use topological sort to find a valid ordering of the letters
  • Check for cycles in the graph, which would indicate an invalid ordering
  • The result of the topological sort is the alien dictionary order

Steps to solve by this approach:

 Step 1: Build a graph where each node is a unique letter from the input words.
 Step 2: Compare adjacent words to determine order relationships between characters.
 Step 3: Create edges in the graph where one character precedes another.
 Step 4: Calculate the in-degree (number of incoming edges) for each node.
 Step 5: Use topological sort via BFS - start with nodes having zero in-degree.
 Step 6: For each visited node, decrease the in-degree of its neighbors.
 Step 7: If we cannot include all characters in our result, a cycle exists, meaning no valid ordering is possible.

Submitted Code :