forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmallest String With Swaps.java
65 lines (57 loc) · 1.61 KB
/
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
60
61
62
63
64
65
class Solution {
int[]parent;
int[]rank;
public String smallestStringWithSwaps(String s, List<List<Integer>> pairs) {
parent = new int[s.length()];
rank = new int[s.length()];
for(int i=0;i<parent.length;i++){
parent[i] = i;
rank[i] = 0;
}
//Union of All Pairs who belongs to same set
for(List<Integer> l : pairs){
int i = l.get(0);
int j = l.get(1);
int il = find(i);
int jl = find(j);
if(il != jl){
union(il,jl);
}
}
//To get the Character in sorted order
PriorityQueue<Character>[]pq = new PriorityQueue[s.length()];
for(int i=0;i<pq.length;i++){
pq[i] = new PriorityQueue<>();
}
for(int i=0;i<s.length();i++){
int il = find(i);
char ch = s.charAt(i);
pq[il].add(ch);
}
StringBuilder sb = new StringBuilder();
for(int i=0;i<s.length();i++){
int il = find(i);
char ch = pq[il].remove();
sb.append(ch);
}
return sb.toString();
}
int find(int x){
if(parent[x] == x){
return x;
}else{
parent[x] = find(parent[x]);
return parent[x];
}
}
void union(int x,int y){
if(rank[x] < rank[y]){
parent[x] = y;
}else if(rank[y] < rank[x]){
parent[y] = x;
}else{
parent[x] = y;
rank[y]++;
}
}
}