-
Notifications
You must be signed in to change notification settings - Fork 1
/
1202+Smallest String With Swaps.cpp
57 lines (44 loc) · 1.38 KB
/
1202+Smallest String With Swaps.cpp
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
const int MAX_N = 1e5 + 5;
class Solution {
public:
struct UF {
int fa[MAX_N];
void init(int n) { for (int i = 0; i < n; ++i) { fa[i] = i; } }
int find (int x) {
if (x == fa[x]) return x;
else {
int F = find(fa[x]);
fa[x] = F;
return F;
}
}
void merge(int x, int y) { fa[find(y)] = find(x); }
} uf;
string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {
if (pairs.size() == 0) return s;
uf.init(s.size());
for (auto pair : pairs) {
uf.merge(pair[0], pair[1]);
}
vector<vector<int>> vec(pairs.size());
vector<vector<char>> vecC(pairs.size());
for (int i = 0; i < s.size(); ++i) {
int tmp = uf.find(i);
vec[uf.find(i)].push_back(i);
vecC[uf.find(i)].push_back(s[i]);
}
for (auto& vecc : vecC) {
sort(vecc.begin(), vecc.end());
}
for (auto& ve : vec) {
sort(ve.begin(), ve.end());
}
string res(s.size(), 0);
for (int i = 0; i < vec.size(); ++i) {
for (int j = 0; j < vec[i].size(); ++j) {
res[vec[i][j]] = vecC[i][j];
}
}
return res;
}
};