-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSmallestStringWithSwaps.java
115 lines (108 loc) · 3.77 KB
/
SmallestStringWithSwaps.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*https://leetcode.com/problems/smallest-string-with-swaps/*/
class Solution {
public String smallestStringWithSwaps(String s, List<List<Integer>> pairs) {
int n = s.length();
int[] parents = IntStream.rangeClosed(0, n-1).toArray();
int[] rank = new int[n];
for (List<Integer> pair : pairs)
union(parents,rank,pair.get(0),pair.get(1));
boolean[] visited = new boolean[n];
char[] arr = s.toCharArray();
Map<Integer,List<Integer>> components = new HashMap<Integer,List<Integer>>();
for (int i = 0; i < n; ++i)
{
int parent = find(parents,i);
components.putIfAbsent(parent,new ArrayList<Integer>());
components.get(parent).add(i);
}
for (Map.Entry entry : components.entrySet())
{
List<Integer> component = (List)entry.getValue();
int[] freqs = new int[26];
for (Integer node : component)
++freqs[arr[node]-'a'];
int index = 0;
while (freqs[index] == 0) ++index;
for (Integer node : component)
{
// System.out.println(index+" "+node);
arr[node] = (char)(index+'a');
--freqs[index];
while (index < 26 && freqs[index] == 0) ++index;
}
}
return new String(arr);
}
private void union(int[] p, int[] r, int x, int y)
{
int px = find(p,x);
int py = find(p,y);
if (px != py)
{
if (r[px] > r[py])
p[py] = px;
else
{
p[px] = py;
if (r[px] == r[py])
++r[py];
}
}
}
private int find(int[] p, int x)
{
if (p[x] == x) return x;
p[x] = find(p,p[x]);
return p[x];
}
}
class Solution {
public String smallestStringWithSwaps(String s, List<List<Integer>> pairs) {
int n = s.length();
Map<Integer,List<Integer>> graph = new HashMap<Integer,List<Integer>>();
for (List<Integer> pair : pairs)
{
int x = pair.get(0), y = pair.get(1);
graph.putIfAbsent(x,new ArrayList<Integer>());
graph.putIfAbsent(y,new ArrayList<Integer>());
graph.get(x).add(y);
graph.get(y).add(x);
}
boolean[] visited = new boolean[n];
List<List<Integer>> components = new ArrayList<List<Integer>>();
for (int i = 0; i < n; ++i)
if (!visited[i])
{
List<Integer> component = new ArrayList<Integer>();
dfs(graph,visited,component,i);
Collections.sort(component);
components.add(component);
}
// System.out.println(components);
char[] arr = s.toCharArray();
for (List<Integer> component : components)
{
int[] freqs = new int[26];
for (Integer node : component)
++freqs[arr[node]-'a'];
int index = 0;
while (freqs[index] == 0) ++index;
for (Integer node : component)
{
// System.out.println(index+" "+node);
arr[node] = (char)(index+'a');
--freqs[index];
while (index < 26 && freqs[index] == 0) ++index;
}
}
return new String(arr);
}
private void dfs(Map<Integer,List<Integer>> graph, boolean[] visited, List<Integer> component, int src)
{
visited[src] = true;
component.add(src);
for (Integer adjNode : graph.getOrDefault(src,new ArrayList<Integer>()))
if (!visited[adjNode])
dfs(graph,visited,component,adjNode);
}
}