-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alien Dictionary.java
107 lines (98 loc) Β· 2.91 KB
/
Alien Dictionary.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
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:
Input:
[
"wrt",
"wrf",
"er",
"ett",
"rftt"
]
Output: "wertf"
Example 2:
Input:
[
"z",
"x"
]
Output: "zx"
Example 3:
Input:
[
"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.
time = O(V+E)
space = O(V)
*/
class Solution {
public String alienOrder(String[] words) {
if (words == null || words.length == 0) {
return "";
}
// construct the graph
Map<Character, Set<Character>> graph = new HashMap<>();
for (int i = 0; i < words.length; i++) {
for (int j = 0; j < words[i].length(); j++) {
char c = words[i].charAt(j);
if (!graph.containsKey(c)) {
graph.put(c, new HashSet<Character>());
}
}
}
for (int i = 0; i < words.length - 1; i++) {
int index = 0;
while (index < words[i].length() && index < words[i + 1].length()) {
if (words[i].charAt(index) != words[i + 1].charAt(index)) {
graph.get(words[i].charAt(index)).add(words[i + 1].charAt(index));
break;
}
index++;
}
}
// topological sorting, indegree
Map<Character, Integer> indegree = new HashMap<>();
for (Character c : graph.keySet()) {
indegree.put(c, 0);
}
for (Character c : graph.keySet()) {
for (Character neighbor : graph.get(c)) {
indegree.put(neighbor, indegree.get(neighbor) + 1);
}
}
Queue<Character> queue = new LinkedList<>();
// find the head
for (Character c : indegree.keySet()) {
if (indegree.get(c) == 0) {
queue.offer(c);
}
}
// outdegree
StringBuilder sb = new StringBuilder();
while (!queue.isEmpty()) {
Character head = queue.poll();
sb.append(head);
for (Character neighbor : graph.get(head)) {
indegree.put(neighbor, indegree.get(neighbor) - 1);
if (indegree.get(neighbor) == 0) {
queue.offer(neighbor);
}
}
}
if (sb.length() != indegree.size()) {
return "";
}
return sb.toString();
}
}