-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path079-passcode-derivation.js
More file actions
76 lines (63 loc) · 1.77 KB
/
079-passcode-derivation.js
File metadata and controls
76 lines (63 loc) · 1.77 KB
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
/**
* Password Derivation
* Time Complexity: O((V + E) log V)
* Space Complexity: O(V + E)
*/
function processData(input) {
input = input.trim().split(/\s+/);
const T = Number(input[0]);
let idx = 1;
const adj = new Map();
const indeg = new Map();
const chars = new Set();
for (let i = 0; i < T; i++) {
const s = input[idx++];
const a = s[0], b = s[1], c = s[2];
chars.add(a); chars.add(b); chars.add(c);
if (!adj.has(a)) adj.set(a, new Set());
if (!adj.has(b)) adj.set(b, new Set());
if (!adj.has(c)) adj.set(c, new Set());
const addEdge = (x, y) => {
if (!adj.get(x).has(y)) {
adj.get(x).add(y);
indeg.set(y, (indeg.get(y) || 0) + 1);
}
};
addEdge(a, b);
addEdge(b, c);
if (!indeg.has(a)) indeg.set(a, 0);
if (!indeg.has(b)) indeg.set(b, indeg.get(b) || 0);
if (!indeg.has(c)) indeg.set(c, indeg.get(c) || 0);
}
const heap = [];
for (let ch of chars) {
if ((indeg.get(ch) || 0) === 0) heap.push(ch);
}
heap.sort();
let result = [];
while (heap.length) {
const u = heap.shift();
result.push(u);
for (let v of adj.get(u) || []) {
indeg.set(v, indeg.get(v) - 1);
if (indeg.get(v) === 0) {
heap.push(v);
heap.sort();
}
}
}
if (result.length !== chars.size) {
console.log("SMTH WRONG");
} else {
console.log(result.join(""));
}
};
process.stdin.resume();
process.stdin.setEncoding("ascii");
let _input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});