-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy path0269. Alien Dictionary.js
108 lines (98 loc) · 2.02 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
// There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of non-empty words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.
//
// Example 1:
//
// Input:
// [
// "wrt",
// "wrf",
// "er",
// "ett",
// "rftt"
// ]
//
// Output: "wertf"
//
// Example 2:
//
// Input:
// [
// "z",
// "x"
// ]
//
// Output: "zx"
//
// Example 3:
//
// Input:
// [
// "z",
// "x",
// "z"
// ]
//
// Output: ""
//
// Explanation: The order is invalid, so return "".
//
// Note:
//
// You may assume all letters are in lowercase.
// You may assume that if a is a prefix of b, then a must appear before b in the given dictionary.
// If the order is invalid, return an empty string.
// There may be multiple valid order of letters, return any one of them is fine.
/**
* @param {string[]} words
* @return {string}
*/
// Topological Sorting + BFS
// Similar
// 210. Course Schedule II
// 269. Alien Dictionary
//
// https://www.youtube.com/watch?v=RIrTuf4DfPE
const alienOrder = (words) => {
// Build graph
const g = {};
const inDegrees = {};
for (const w of words) {
for (const c of w) {
inDegrees[c] = 0;
g[c] = new Set();
}
}
for (let i = 1; i < words.length; i++) {
const w1 = words[i - 1];
const w2 = words[i];
const len = Math.min(w1.length, w2.length);
for (let j = 0; j < len; j++) {
const c1 = w1[j];
const c2 = w2[j];
if (c1 !== c2) {
if (!g[c1].has(c2)) {
g[c1].add(c2);
inDegrees[c2]++;
}
break;
}
}
}
// BFS
let s = '';
const q = [];
for (const c in inDegrees) {
if (inDegrees[c] === 0) q.push(c);
}
while (q.length) {
const c1 = q.shift();
s += c1;
for (const c2 of g[c1]) {
inDegrees[c2]--;
if (inDegrees[c2] === 0) {
q.push(c2);
}
}
}
return s.length === Object.keys(g).length ? s : '';
};