Gist:
Given a sorted dictionary (array of words) of an alien language, find order of characters in the language.
Detailed:
There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of non-empty words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.
Input:["wrt","wrf","er","ett","rftt"]Output:"wertf"Explanation:from "wrt"and"wrf",we can get 't'<'f'from "wrt"and"er",we can get 'w'<'e'from "er"and"ett",we can get 'r'<'t'from "ett"and"rftt",we can get 'e'<'r'So return"wertf"
Example 2:
1
2
3
4
5
Input:["z","x"]Output:"zx"Explanation:from "z" and "x",we can get 'z'<'x'So return"zx"
First of all, we need to understand that order of words is very important, since as it is told in the task: words are sorted lexicographically by the rules of this new language.
That means that in case [“wrt”, “ert”] “w” should come before “e”.
Also, we need to make sure that length of the word can also be different.
So, we have to form the graph based on the chars in the list of words. Source will be char from word[i] and destination will be char from word [i+x] for eg.
Then if there is cycle, we return “”. If not, we return topological sort of string. Also, if the graph is not strongly connected, we can have multiple solutions.
Also, normal DFS will not work. For eg. Look at this graph:
graph LR;
A --> B & C
B --> C
We can get 2 answers - ABC, ACB. But ACB is wrong, as C is dependant on B.
Hence, need to do something call post order DFS. Look at Topological Sorting.
⏰ Time complexity: O(N * L + V + E), where N is the number of words and L is the average length of a word, V is the number of unique characters and E is the number of precedence edges.
Graph Construction: O(N * L)
DFS Traversal: O(V + E)`
🧺 Space complexity: O(V + E) for graph storage. O(V) for Visited and recursion stack. Overall O(V+E)