forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
59 lines (56 loc) · 1.54 KB
/
Solution.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 {
private Map<Integer, Integer> p;
private Map<Integer, Integer> size;
private int mx;
private int n;
public int[] groupStrings(String[] words) {
p = new HashMap<>();
size = new HashMap<>();
n = words.length;
mx = 0;
for (String word : words) {
int x = 0;
for (char c : word.toCharArray()) {
x |= 1 << (c - 'a');
}
p.put(x, x);
size.put(x, size.getOrDefault(x, 0) + 1);
mx = Math.max(mx, size.get(x));
if (size.get(x) > 1) {
--n;
}
}
for (int x : p.keySet()) {
for (int i = 0; i < 26; ++i) {
union(x, x ^ (1 << i));
if (((x >> i) & 1) != 0) {
for (int j = 0; j < 26; ++j) {
if (((x >> j) & 1) == 0) {
union(x, x ^ (1 << i) | (1 << j));
}
}
}
}
}
return new int[] {n, mx};
}
private int find(int x) {
if (p.get(x) != x) {
p.put(x, find(p.get(x)));
}
return p.get(x);
}
private void union(int a, int b) {
if (!p.containsKey(b)) {
return;
}
int pa = find(a), pb = find(b);
if (pa == pb) {
return;
}
p.put(pa, pb);
size.put(pb, size.get(pb) + size.get(pa));
mx = Math.max(mx, size.get(pb));
--n;
}
}