-
-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathalienDictionary.js
61 lines (50 loc) · 1.56 KB
/
alienDictionary.js
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
//BFS(Topological sorting through Kahn's algorithm):- TC: O(m*n) SC:O(m*n)
function alienOrder(words) {
let adjList = {};
let inDegree = Array(26).fill(0);
let uniqueLetters = new Set();
for(let i=0; i< words.length-1; i++) {
let currWord = words[i];
let nextWord = words[i+1];
if(currWord.length > nextWord.length && currWord.startsWith(nextWord)) {
return "";
}
for(let j=0; j< Math.min(currWord.length, nextWord.length); j++) {
let ch1 = currWord[j];
let ch2 = nextWord[j];
if(ch1 !== ch2) {
if(!adjList[ch1]) {
adjList[ch1] = [];
}
adjList[ch1].push(ch2);
inDegree[ch2.charCodeAt(0)-97]++;
break;
}
}
}
for(let word of words) {
for(let ch of word) {
uniqueLetters.add(ch);
}
}
let queue = [];
for(let ch of uniqueLetters) {
if(inDegree[ch.charCodeAt(0)-97] === 0) {
queue.push(ch);
}
}
let topStr = "";
while(queue.length >0) {
let currChar = queue.shift();
topStr += currChar;
if(!adjList[currChar]) continue;
for(let neighbour of adjList[currChar]) {
if(--inDegree[neighbour.charCodeAt(0)-97] === 0) {
queue.push(neighbour);
}
}
}
return topStr.length === uniqueLetters.size ? topStr : "";
}
let words = ["wrt", "wrf", "er", "ett", "rftt"];
console.log(alienOrder(words));