forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0269-alien-dictionary.js
179 lines (133 loc) · 4.27 KB
/
0269-alien-dictionary.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* BFS
* https://leetcode.com/problems/alien-dictionary/
* @param {string[]} words
* @return {string}
*/
var alienOrder = function(words) {
const { graph, frequencyMap, queue, buffer } = buildGraph(words);
if (!canBuildGraph(words, graph, frequencyMap)) return '';
queueSources(queue, frequencyMap);
bfs(queue, frequencyMap, graph, buffer);
return (frequencyMap.size <= buffer.length)
? buffer.join('')
: '';
}
var initGraph = () => ({
graph: new Map(),
frequencyMap: new Map(),
queue: new Queue(),
buffer: [],
})
var buildGraph = (words) => {
const { graph, frequencyMap, queue, buffer } = initGraph();
for (const word of words) {
for (const char of word) {
frequencyMap.set(char, 0);
graph.set(char, []);
}
}
return { graph, frequencyMap, queue, buffer };
};
var canBuildGraph = (words, graph, frequencyMap) => {
for (let index = 0; (index < words.length - 1); index++) {
const [ word1, word2 ] = [ words[index], words[(index + 1)] ];
const minLength = Math.min(word1.length, word2.length)
const isWord1Longer = (word2.length < word1.length);
const isPrefix = isWord1Longer && word1.startsWith(word2);
if (isPrefix) return false;
for (let j = 0; (j < minLength); j++) {
const [ char1, char2 ] = [ word1[j], word2[j] ];
const isEqual = (char1 === char2);
if (isEqual) continue;
graph.get(char1).push(char2);
frequencyMap.set(char2, frequencyMap.get(char2) + 1);
break;
}
}
return true;
};
const bfs = (queue, frequencyMap, graph, buffer) => {
while (!queue.isEmpty()) {
for (let level = (queue.size() - 1); (0 <= level); level--) {
checkNeighbors(queue, frequencyMap, graph, buffer)
}
}
};
var checkNeighbors = (queue, frequencyMap, graph, buffer) => {
const char = queue.dequeue();
buffer.push(char);
for (const next of graph.get(char)) {
const value = (frequencyMap.get(next) - 1);
frequencyMap.set(next, value);
const isEmpty = (frequencyMap.get(next) === 0);
if (!isEmpty) continue;
queue.enqueue(next);
}
}
const queueSources = (queue, frequencyMap) => {
for (const [ key, value ] of frequencyMap) {
const isEmpty = (frequencyMap.get(key) === 0);
if (!isEmpty) continue;
queue.enqueue(key);
}
}
/**
* DFS
* https://leetcode.com/problems/alien-dictionary/
* @param {string[]} words
* @return {string}
*/
var alienOrder = function(words) {
const { graph, seen, buffer } = buildGraph(words);
if (!canBuildGraph(words, graph)) return '';
for (const [ char ] of graph) {
if (!dfs(char, graph, seen, buffer)) return '';
}
return buffer.reverse().join('')
}
var initGraph = () => ({
graph: new Map(),
seen: new Map(),
buffer: [],
})
var buildGraph = (words) => {
const { graph, seen, buffer } = initGraph();
for (const word of words) {
for (const char of word) {
graph.set(char, []);
}
}
return { graph, seen, buffer };
};
var canBuildGraph = (words, graph) => {
for (let index = 0; (index < words.length - 1); index++) {
const [ word1, word2 ] = [ words[index], words[(index + 1)] ];
const minLength = Math.min(word1.length, word2.length)
const isWord1Longer = (word2.length < word1.length);
const isPrefix = isWord1Longer && word1.startsWith(word2);
if (isPrefix) return false;
for (let j = 0; (j < minLength); j++) {
const [ char1, char2 ] = [ word1[j], word2[j] ];
const isEqual = (char1 === char2);
if (isEqual) continue;
graph.get(char1).push(char2);
break;
}
}
return true;
};
const dfs = (char, graph, seen, buffer) => {
if (seen.has(char)) return seen.get(char);
if (!backTrack(char, graph, seen, buffer)) return false;
buffer.push(char);
return true;
}
const backTrack = (char, graph, seen, buffer) => {
seen.set(char, false);
for (const neighbor of graph.get(char)) {
if (!dfs(neighbor, graph, seen, buffer)) return false;
}
seen.set(char, true);
return true;
}