269. Alien Dictionary
Problem description:
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.
Example 1:1
2
3
4
5
6
7
8
9
10Input:
[
"wrt",
"wrf",
"er",
"ett",
"rftt"
]
Output: "wertf"
Example 2:1
2
3
4
5
6
7Input:
[
"z",
"x"
]
Output: "zx"
Example 3:1
2
3
4
5
6
7
8
9
10Input:
[
"z",
"x",
"z"
]
Output: ""
Explanation: The order is invalid, so return "".
Note:
You may assume all letters are in lowercase.
You may assume that if a is a prefix of b, then a must appear before b in the given dictionary.
If the order is invalid, return an empty string.
There may be multiple valid order of letters, return any one of them is fine.
Solution:
- find characters in words
- build graph and indegree, graph contains all neighbors of a characters
c
, indegree is how many prerequisite need to complete before this characterc
- find character if it’s indegree is
0
- topological sort, remove indegree count for neighbor when we complete
c
- if the result length is not equal to indegree size, then return
""
1 | class Solution: |
First, build a degree map for each character in all the words:1
2
3
4
5w:0
r:0
t:0
f:0
e:0
Then build the hashmap by comparing the adjacent words, the first character that is different between two adjacent words reflect the lexicographical order. For example:1
2
3
4
5
6
7"wrt",
"wrf",
first different character is 3rd letter, so t comes before f
"wrf",
"er",
first different character is 1rd letter, so w comes before e
The characters in set come after the key. x->y means letter x comes before letter y. x -> set: y,z,t,w means x comes before all the letters in the set. The final HashMap “map” looks like.1
2
3
4t -> set: f
w -> set: e
r -> set: t
e -> set: r
and final HashMap “degree” looks like, the number means “how many letters come before the key”:1
2
3
4
5w:0
r:1
t:1
f:1
e:1
1 | class Solution { |
time complexity: $O(n+V))$, n: words averge length, V: alpha number of characters in given alphabet
space complexity: $O(V)$
The first step to create a graph takes O(n + alhpa) time where n is number of given words and alpha is number of characters in given alphabet. The second step is also topological sorting. Note that there would be alpha vertices and at-most (n-1) edges in the graph. The time complexity of topological sorting is O(V+E) which is O(n + aplha) here. So overall time complexity is O(n + aplha) + O(n + aplha) which is O(n + aplha).
Second time:
Question to ask:
- word in same length?
- contain duplicate?
- only character?
1 | class Solution { |
reference:
https://goo.gl/fKXTmg
https://goo.gl/DDsvde
https://goo.gl/Lb26cZ