-
Notifications
You must be signed in to change notification settings - Fork 481
/
1202.cpp
39 lines (34 loc) · 921 Bytes
/
1202.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
class Solution
{
public:
string smallestStringWithSwaps(string s, vector<vector<int>>& pairs)
{
for (int i = 0; i < s.size(); i++) p.push_back(i);
for (auto& it : pairs)
{
int px = find(it[0]), py = find(it[1]);
if (px != py) p[px] = py;
}
unordered_map<int, vector<int>> mem;
for (int i = 0; i < p.size(); i++)
{
mem[find(p[i])].push_back(s[i]);
}
for (auto& it : mem) sort(it.second.begin(), it.second.end(), greater<int>());
string res;
for (int i = 0; i < s.size(); i++)
{
int x = find(i);
res.push_back(mem[x].back());
mem[x].pop_back();
}
return res;
}
private:
vector<int> p;
int find(int x)
{
if (x != p[x]) p[x] = find(p[x]);
return p[x];
}
};