-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path1202-smallest-string-with-swaps.js
65 lines (58 loc) · 1.73 KB
/
1202-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
55
56
57
58
59
60
61
62
63
64
65
/**
* 1202. Smallest String With Swaps
* https://leetcode.com/problems/smallest-string-with-swaps/
* Difficulty: Medium
*
* You are given a string s, and an array of pairs of indices in the string pairs where
* pairs[i] = [a, b] indicates 2 indices(0-indexed) of the string.
*
* You can swap the characters at any pair of indices in the given pairs any number of times.
*
* Return the lexicographically smallest string that s can be changed to after using the swaps.
*/
/**
* @param {string} s
* @param {number[][]} pairs
* @return {string}
*/
var smallestStringWithSwaps = function(s, pairs) {
const parent = Array.from({ length: s.length }, (_, i) => i);
const rank = new Array(s.length).fill(0);
function find(x) {
if (parent[x] !== x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
pairs.forEach(([x, y]) => union(x, y));
const components = new Map();
for (let i = 0; i < s.length; i++) {
const root = find(i);
if (!components.has(root)) {
components.set(root, { indices: [], chars: [] });
}
components.get(root).indices.push(i);
components.get(root).chars.push(s[i]);
}
const result = new Array(s.length);
for (const { indices, chars } of components.values()) {
chars.sort((a, b) => b.localeCompare(a));
indices.sort((a, b) => a - b);
indices.forEach((idx, i) => result[idx] = chars.pop());
}
return result.join('');
function union(x, y) {
const rootX = find(x);
const rootY = find(y);
if (rootX !== rootY) {
if (rank[rootX] < rank[rootY]) {
parent[rootX] = rootY;
} else if (rank[rootX] > rank[rootY]) {
parent[rootY] = rootX;
} else {
parent[rootY] = rootX;
rank[rootX]++;
}
}
}
};