1
var smallestStringWithSwaps = function (s, pairs) {2
const uf = new UnionFind(s.length);3
pairs.forEach(([x, y]) => uf.union(x, y));6
for (const [root, charIndex] of Object.entries(uf.disjointSets())) {7
let chars = charIndex.map((i) => s[i]);9
charIndex.forEach((charIndex, i) => (result[charIndex] = chars[i]));12
return result.join("");17
this.roots = Array.from({ length: len }).map((_, i) => i);18
this.rank = Array.from({ length: len }).fill(1);22
if (x == this.roots[x]) {25
return (this.roots[x] = this.find(this.roots[x]));29
let rootX = this.find(x);30
let rootY = this.find(y);32
if (this.rank[rootX] > this.rank[rootY]) {33
this.roots[rootY] = rootX;34
} else if (this.rank[rootX] < this.rank[rootY]) {35
this.roots[rootX] = rootY;38
this.roots[rootY] = rootX;45
for (let i = 0; i < this.roots.length; i++) {46
let currentRoot = this.find(i);47
if (currentRoot in ds) {48
ds[currentRoot].push(i);50
ds[currentRoot] = [i];