Skip to content

Commit 15a2da5

Browse files
committed
Runtime: 370 ms (Top 35.98%) | Memory: 87.5 MB (Top 5.47%)
1 parent 84066cc commit 15a2da5

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,49 @@
1+
// Runtime: 370 ms (Top 35.98%) | Memory: 87.5 MB (Top 5.47%)
12
class Solution {
23
public:
3-
4+
45
vector<int> parent;
5-
6+
67
int findParent(int n)
78
{
89
if(parent[n] == n) return n;
910
return parent[n] = findParent(parent[n]);
1011
}
11-
12+
1213
string smallestStringWithSwaps(string s, vector<vector<int>>& pairs)
1314
{
1415
map<int, set<int>> mp;
1516
parent.resize(s.size());
1617
string ans = s;
17-
18+
1819
for(int i=0; i<s.length(); i++) parent[i] = i;
19-
20+
2021
for(auto pair: pairs)
2122
{
2223
int p1 = findParent(pair[0]), p2 = findParent(pair[1]);
2324
if(p1 != p2) parent[p2] = p1;
2425
}
25-
26+
2627
for(auto pair: pairs)
2728
{
2829
int p = findParent(pair[0]);
2930
mp[p].insert(pair[0]);
3031
mp[p].insert(pair[1]);
3132
}
32-
33+
3334
for(auto it: mp)
3435
{
3536
vector<char> part;
3637
set<int> idx = it.second;
37-
38+
3839
for(auto index: idx) part.push_back(s[index]);
39-
40+
4041
sort(part.begin(), part.end());
41-
42+
4243
auto index = idx.begin();
43-
for(auto x: part) ans[*index] = x, ++index;
44+
for(auto x: part) ans[*index] = x, ++index;
4445
}
4546

4647
return ans;
4748
}
48-
};
49+
};

0 commit comments

Comments
 (0)