forked from szl0072/Leetcode-Solution-Code
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlienDictionary.java
126 lines (105 loc) · 2.97 KB
/
AlienDictionary.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package leetcode;
import java.util.*;
/**
* Project Name : Leetcode
* Package Name : leetcode
* File Name : AlienDictionary
* Creator : Edward
* Date : Dec, 2017
* Description : 269. Alien Dictionary
*/
public class AlienDictionary {
/**
* 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:
Given the following words in dictionary,
[
"wrt",
"wrf",
"er",
"ett",
"rftt"
]
The correct order is: "wertf".
Example 2:
Given the following words in dictionary,
[
"z",
"x"
]
The correct order is: "zx".
Example 3:
Given the following words in dictionary,
[
"z",
"x",
"z"
]
The order is invalid, so return "".
图 -> 入度为0 -> BFS
count = 5
degree :
w : 1
e : 1
r : 1
t : 1
f : 2
time : (V + E) -> O(n * words(max))
space : O(n) -> O(26) -> O(1)
* @param words
* @return
*/
public static String alienOrder(String[] words) {
if (words == null || words.length == 0) return "";
StringBuilder res = new StringBuilder();
HashMap<Character, Set<Character>> map = new HashMap<>();
int[] degree = new int[26];
int count = 0;
for (String word : words) {
for (char c : word.toCharArray()) {
if (degree[c - 'a'] == 0) {
count++;
degree[c - 'a'] = 1;
}
}
}
for (int i = 0; i < words.length - 1; i++) {
char[] cur = words[i].toCharArray();
char[] next = words[i + 1].toCharArray();
int len = Math.min(cur.length, next.length);
for (int j = 0; j < len; j++) {
if (cur[j] != next[j]) {
if (!map.containsKey(cur[j])) {
map.put(cur[j], new HashSet<>());
}
if (map.get(cur[j]).add(next[j])) {
degree[next[j] - 'a']++;
}
break;
}
}
}
Queue<Character> queue = new LinkedList<>();
for (int i = 0; i < 26; i++) {
if (degree[i] == 1) {
queue.offer((char)('a' + i));
}
}
while (!queue.isEmpty()) {
char c = queue.poll();
res.append(c);
if (map.containsKey(c)) {
for (char ch : map.get(c)) {
if (--degree[ch - 'a'] == 1) {
queue.offer(ch);
}
}
}
}
if (res.length() != count) return "";
return res.toString();
}
}