-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1202-smallest-string-with-swaps.cpp
More file actions
78 lines (66 loc) · 1.86 KB
/
Copy path1202-smallest-string-with-swaps.cpp
File metadata and controls
78 lines (66 loc) · 1.86 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
77
78
#include <unordered_map>
#include <vector>
#include <queue>
#include <string>
using namespace std;
class Solution {
public:
template <typename K, typename V>
using m = unordered_map<K, V>;
template <typename T>
using v = vector<T>;
v<int> parent;
v<int> size;
int find_set(int i) {
if (parent[i] == -1) {
make_set(i);
return i;
}
if (parent[i] == i)
return i;
return parent[i] = find_set(parent[i]);
}
void make_set(int i) {
parent[i] = i;
size[i] = 1;
}
void union_sets(int a, int b) {
int sa = find_set(a), sb = find_set(b);
if (sa != sb) {
// attach smaller rank onto larger rank
if (size[sa] < size[sb]) {
parent[sa] = sb;
size[sb] += size[sa];
} else {
parent[sb] = sa;
size[sa] += size[sb];
}
}
}
string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {
parent.resize(s.size(), -1);
size.resize(s.size(), -1);
for (int k = 0; k < pairs.size(); ++k) {
int i = pairs[k][0], j = pairs[k][1];
int si = find_set(i), sj = find_set(j);
union_sets(si, sj);
}
// iterate disjoint sets
m<int, priority_queue<char, vector<char>, greater<char>>> strings; // strings[parent] -> sorted string
for (int i = 0; i < s.size(); ++i) {
int p = find_set(i);
if (p == -1)
continue;
strings[p].push(s[i]);
}
string res = s;
for (int i = 0; i < s.size(); ++i) {
int p = find_set(i);
if (p == -1)
continue;
res[i] = strings[p].top();
strings[p].pop();
}
return res;
}
};