forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmallest String With Swaps.js
54 lines (47 loc) · 1.4 KB
/
Smallest String With Swaps.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
var smallestStringWithSwaps = function(s, pairs) {
const uf = new UnionFind(s.length);
pairs.forEach(([x,y]) => uf.union(x,y))
const result = [];
for (const [root, charIndex] of Object.entries(uf.disjointSets())) {
let chars = charIndex.map(i => s[i])
chars.sort();
charIndex.forEach((charIndex, i) => result[charIndex] = chars[i])
}
return result.join("")
};
class UnionFind {
constructor(len) {
this.roots = Array.from({length: len}).map((_, i) => i);
this.rank = Array.from({length: len}).fill(1);
}
find(x) {
if (x == this.roots[x]) {
return x;
}
return this.roots[x] = this.find(this.roots[x])
}
union(x, y) {
let rootX = this.find(x);
let rootY = this.find(y);
if (this.rank[rootX] > this.rank[rootY]) {
this.roots[rootY] = rootX;
} else if (this.rank[rootX] < this.rank[rootY]) {
this.roots[rootX] = rootY;
} else { // ranks equal
this.roots[rootY] = rootX;
this.rank[rootX]++
}
}
disjointSets() {
const ds = {};
for(let i = 0; i < this.roots.length; i++) {
let currentRoot = this.find(i);
if (currentRoot in ds) {
ds[currentRoot].push(i);
} else {
ds[currentRoot] = [i];
}
}
return ds;
}
}