-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathalienDictionary.js
121 lines (99 loc) · 2.86 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
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
//BFS(Topological sorting through Kahn's algorithm):- TC: O(V+E) SC:O(V+E)
function alienOrder1(words) {
let adjList = {};
let inDegree = Array(26).fill(0);
let uniqueLetters = new Set();
for (let i = 0; i < words.length - 1; i++) {
let firstWord = words[i];
let secondWord = words[i + 1];
if (firstWord.length > secondWord.length && firstWord.startsWith(secondWord)) {
return "";
}
for (let j = 0; j < Math.min(firstWord.length, secondWord.length); j++) {
let ch1 = firstWord[j];
let ch2 = secondWord[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 : "";
}
//DFS using recursion:- TC: O(V+E) SC:O(V+E)
function alienOrder2(words) {
let adjList = {};
let result = [];
words.forEach((word) => {
for(const ch of word){
adjList[ch] = new Set();
}
});
for (let i = 0; i < words.length-1; i++) {
const firstWord = words[i];
const secondWord = words[i+1];
if(firstWord.length > secondWord.length && firstWord.startsWith(secondWord)) {
return "";
}
for(let j=0; j < Math.min(firstWord.length, secondWord.length); j++){
let ch1 = firstWord[j];
let ch2 = secondWord[j];
if(ch1 !== ch2) {
adjList[ch1].add(ch2);
break;
}
}
}
let visitState = {}; // 0 = unvisited, 1 = visited, 2 = processed
for(const ch in adjList) {
if(!visitState[ch] && dfs(ch, adjList, visitState, result)) return "";
}
return result.reverse().join("");
}
function dfs(ch, adjList, visitState, result){
if(visitState[ch] === 1) {
return true;
}
if(visitState[ch] === 2) {
return false;
}
visitState[ch] = 1;
for (const neighbour of adjList[ch]) {
if(dfs(neighbour, adjList, visitState, result)) return true;
}
visitState[ch] = 2;
result.push(ch);
return false;
}
let words1 = ["grs", "grk", "dr", "dss", "rkss"];
console.log(alienOrder1(words1));
console.log(alienOrder2(words1));
let words2 = ["t", "r"];
console.log(alienOrder1(words2));
console.log(alienOrder2(words2));