
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".
- Step 1: Compare adjacent words to find character relationships.
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".
- Step 1: Compare adjacent words to find character relationships.
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
Editorial
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. |
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