-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathAlienDictionary.java
66 lines (56 loc) · 2.12 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
package java1.algorithms.graph;
import java.util.*;
public class AlienDictionary {
//BFS(Topological sorting through Kahn's alogirthm):- TC: O(m*n) SC:O(m*n)
private static String alienOrder(String[] words) {
if(words == null || words.length == 0) return null;
Map<Character, List<Character>> adjList = new HashMap<>();
int[] inDegree = new int[26];
for(int i=0; i< words.length-1; i++) {
String currWord = words[i], nextWord = words[i+1];
if(currWord.length() > nextWord.length() && currWord.substring(0, nextWord.length()).equals(nextWord)) {
return "";
}
for(int j=0; j< Math.min(currWord.length(), nextWord.length()); j++) {
char ch1 = currWord.charAt(j);
char ch2 = nextWord.charAt(j);
if(ch1 != ch2) {
if(!adjList.containsKey(ch1)) {
adjList.put(ch1, new ArrayList<>());
}
adjList.get(ch1).add(ch2);
inDegree[ch2- 'a']++;
break;
}
}
}
Set<Character> uniqueLetters = new HashSet<>();
for(String word: words) {
for(char ch: word.toCharArray()) {
uniqueLetters.add(ch);
}
}
PriorityQueue<Character> queue = new PriorityQueue<>();
for(char ch: uniqueLetters) {
if(inDegree[ch-'a'] == 0) {
queue.add(ch);
}
}
String topSort = "";
while(!queue.isEmpty()) {
char ch = queue.poll();
topSort += ch;
if(adjList.get(ch) == null) continue;
for(char neighbour: adjList.get(ch)) {
if(--inDegree[neighbour-'a'] == 0) {
queue.add(neighbour);
}
}
}
return topSort.length() == uniqueLetters.size() ? topSort : "";
}
public static void main(String[] args) {
String[] words = {"wrt", "wrf", "er", "ett", "rftt"};
System.out.println(alienOrder(words));
}
}