-
Notifications
You must be signed in to change notification settings - Fork 1
/
alien-dictionary.java
54 lines (47 loc) · 1.7 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
class Solution {
public String findOrder(String[] dict, int n, int k) {
// Create adjacency list
List<Integer>[] adj = new ArrayList[k];
for (int i = 0; i < k; i++) {
adj[i] = new ArrayList<>();
}
// Build the graph by comparing adjacent words
for (int i = 0; i < n - 1; i++) {
String word1 = dict[i];
String word2 = dict[i + 1];
int len = Math.min(word1.length(), word2.length());
for (int j = 0; j < len; j++) {
if (word1.charAt(j) != word2.charAt(j)) {
adj[word1.charAt(j) - 'a'].add(word2.charAt(j) - 'a');
break; // stop at the first different character
}
}
}
// Perform topological sort using Kahn's algorithm (BFS)
int[] inDegree = new int[k];
for (int i = 0; i < k; i++) {
for (int neighbor : adj[i]) {
inDegree[neighbor]++;
}
}
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < k; i++) {
if (inDegree[i] == 0) {
queue.add(i);
}
}
StringBuilder order = new StringBuilder();
while (!queue.isEmpty()) {
int curr = queue.poll();
order.append((char) (curr + 'a'));
for (int neighbor : adj[curr]) {
inDegree[neighbor]--;
if (inDegree[neighbor] == 0) {
queue.add(neighbor);
}
}
}
// If order contains all k characters, return it, otherwise return an empty string
return order.length() == k ? order.toString() : "";
}
}