Skip to content

Commit 98b4999

Browse files
solves 1202: Smallest String With Swaps in java
1 parent 65f5f54 commit 98b4999

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@
523523
| 1196 | 🔒 [How Many Apples Can You Put into the Basket](https://leetcode.com/problems/how-many-apples-can-you-put-into-the-basket) | | |
524524
| 1200 | [Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference) | [![Java](assets/java.png)](src/MinimumAbsoluteDifference.java) | |
525525
| 1201 | [Ugly Number III](https://leetcode.com/problems/ugly-number-iii) | [![Java](assets/java.png)](src/UglyNumberIII.java) | |
526+
| 1202 | [Smallest String With Swaps](https://leetcode.com/problems/smallest-string-with-swaps) | [![Java](assets/java.png)](src/SmallestStringWithSwaps.java) | |
526527
| 1207 | [Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences) | [![Java](assets/java.png)](src/UniqueNumberOfOccurrences.java) | |
527528
| 1213 | 🔒 [Intersection of Three Sorted Arrays](https://leetcode.com/problems/intersection-of-three-sorted-arrays) | | |
528529
| 1217 | [Minimum Cost to Move Chips to The Same Position](https://leetcode.com/problems/minimum-cost-to-move-chips-to-the-same-position) | [![Java](assets/java.png)](src/MinimumCostToMoveChipsToTheSamePosition.java) | |

Diff for: src/SmallestStringWithSwaps.java

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// https://leetcode.com/problems/smallest-string-with-swaps
2+
// s = |s|, p = |pairs|
3+
// T: O(s + p * al(s) + s log(s)) al = inverse Ackermann function
4+
// S: O(s + log(s))
5+
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.PriorityQueue;
10+
import java.util.Queue;
11+
12+
public class SmallestStringWithSwaps {
13+
private static final class DisjointSet {
14+
private final int[] root, rank;
15+
16+
public DisjointSet(int size) {
17+
root = new int[size];
18+
rank = new int[size];
19+
for (int i = 0 ; i < size ; i++) {
20+
root[i] = i;
21+
rank[i] = 1;
22+
}
23+
}
24+
25+
public int find(int num) {
26+
if (num == root[num]) {
27+
return num;
28+
}
29+
return root[num] = find(root[num]);
30+
}
31+
32+
public boolean areConnected(int x, int y) {
33+
return find(x) == find(y);
34+
}
35+
36+
public void union(int x, int y) {
37+
final int rootX = find(x), rootY = find(y);
38+
if (rootX == rootY) {
39+
return;
40+
}
41+
if (rank[rootX] > rank[rootY]) {
42+
root[rootY] = rootX;
43+
} else if (rank[rootX] < rank[rootY]) {
44+
root[rootX] = rootY;
45+
} else {
46+
root[rootY] = rootX;
47+
rank[rootX]++;
48+
}
49+
}
50+
}
51+
52+
public String smallestStringWithSwaps(String s, List<List<Integer>> pairs) {
53+
final DisjointSet disjointSet = new DisjointSet(s.length());
54+
for (List<Integer> pair : pairs) {
55+
disjointSet.union(pair.get(0), pair.get(1));
56+
}
57+
final Map<Integer, Queue<Character>> charMinHeaps = new HashMap<>();
58+
for (int i = 0 ; i < s.length() ; i++) {
59+
final int root = disjointSet.find(i);
60+
final Queue<Character> minHeap = charMinHeaps.getOrDefault(root, new PriorityQueue<>());
61+
minHeap.add(s.charAt(i));
62+
charMinHeaps.putIfAbsent(root, minHeap);
63+
}
64+
final StringBuilder builder = new StringBuilder();
65+
for (int i = 0 ; i < s.length() ; i++) {
66+
final int root = disjointSet.find(i);
67+
builder.append(charMinHeaps.get(root).poll());
68+
}
69+
return builder.toString();
70+
}
71+
}

0 commit comments

Comments
 (0)