-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathSmallest String With Swaps.cpp
63 lines (47 loc) · 1.75 KB
/
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
58
59
60
61
62
63
/*
Solution by Rahul Surana
***********************************************************
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.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
int parent(int u, vector<int> &p){
if(p[u] == u) return u;
return p[u] = parent(p[p[u]],p);
}
string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {
vector<int> p(s.length()+1,0);
map<int,vector<int>> m;
map<int,int> mi;
for(int i = 0 ; i < s.length(); i++) p[i] = i;
for(int i = 0 ; i < pairs.size(); i++){
int a = parent(pairs[i][0],p);
int b = parent(pairs[i][1],p);
p[a] = b;
}
for(int i = 0 ; i < s.length(); i++){
int a = parent(i,p);
m[a].push_back(i);
mi[a] = 0;
}
for(auto &x: m){
sort(x.second.begin(),x.second.end(),[&](int a, int b){ return s[a]<s[b]; });
// for(auto z: x.second) cout << z <<" ";
// cout << "\n";
}
string ans;
for(int i = 0 ; i < s.length(); i++){
int a = parent(i,p);
ans += s[m[a][mi[a]]];
// cout << m[a][mi[a]] <<" "<< mi[a]<<" ";
mi[a]++;
// m[a].erase(m[a].begin());
}
return ans;
}
};