-
Notifications
You must be signed in to change notification settings - Fork 0
/
1202-smallest-string-with-swaps.java
59 lines (50 loc) · 1.67 KB
/
1202-smallest-string-with-swaps.java
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
class Solution {
public String smallestStringWithSwaps(String s, List<List<Integer>> pairs) {
UnionFind uf = new UnionFind(s.length());
for (int i = 0; i < pairs.size(); i++) {
int a = pairs.get(i).get(0);
int b = pairs.get(i).get(1);
uf.unionAll(a, b);
}
Map<Integer, List<Integer>> graphGroup = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
int root = uf.find(i);
graphGroup.putIfAbsent(root, new ArrayList<>());
graphGroup.get(root).add(i);
}
char[] charArr = new char[s.length()];
for (List<Integer> item : graphGroup.values()) {
List<Character> charList = new ArrayList<>();
for (int index : item) {
charList.add(s.charAt(index));
}
Collections.sort(charList);
for (int idx = 0; idx < item.size(); idx++) {
charArr[item.get(idx)] = charList.get(idx);
}
}
return new String(charArr);
}
public class UnionFind {
private int[] parent;
public UnionFind(int size) {
parent = new int[size];
for (int i = 0; i < size; i++) {
parent[i] = i;
}
}
public int find(int n) {
if (n == parent[n]) {
return n;
} else {
return parent[n] = find(parent[n]);
}
}
public void unionAll(int a, int b) {
int x = find(a);
int y = find(b);
if (x >= y) parent[x] = parent[y];
else parent[y] = parent[x];
}
}
}