forked from Mohammed-Shoaib/Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC1202.cpp
More file actions
executable file
·52 lines (43 loc) · 1.15 KB
/
LC1202.cpp
File metadata and controls
executable file
·52 lines (43 loc) · 1.15 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
/*
Problem Statement: https://leetcode.com/problems/smallest-string-with-swaps/
Time: O(m + n • log n)
Space: O(m + n)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
public:
string smallestStringWithSwaps(string s, vector<vector<int>>& pairs) {
int n = s.length(), m = pairs.size();
vector<int> component;
vector<bool> visited(n);
vector<vector<int>> adj(n);
// helper function
function<void(int)> dfs = [&](int s) {
visited[s] = true;
component.push_back(s);
for (auto u: adj[s])
if(!visited[u])
dfs(u);
};
// create adjacency list
for (int i = 0; i < pairs.size(); i++) {
adj[pairs[i][0]].push_back(pairs[i][1]);
adj[pairs[i][1]].push_back(pairs[i][0]);
}
// get all the components and sort them
for (int i = 0; i < n; i++) {
if (visited[i])
continue;
string substr;
component.clear();
dfs(i);
for (int i = 0; i < component.size(); i++)
substr += s[component[i]];
sort(substr.begin(), substr.end());
sort(component.begin(), component.end());
for (int i = 0; i < component.size(); i++)
s[component[i]] = substr[i];
}
return s;
}
};