-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path990.satisfiability-of-equality-equations.java
75 lines (67 loc) · 1.81 KB
/
990.satisfiability-of-equality-equations.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
/*
* @lc app=leetcode id=990 lang=java
*
* [990] Satisfiability of Equality Equations
*/
// @lc code=start
class Solution6 {
public static void main(String[] args) {
Solution6 s = new Solution6();
System.out.println(s.equationsPossible(new String[]{"c==c","b==d","x!=z"
}));
}
private int[] id, sz;
public void UnionFind(int n) {
id = new int[n];
sz = new int[n];
for (int i = 0; i < n; i++) {
id[i] = i;
sz[i] = 1;
}
}
public int find(int p) {
int root = p;
while (root != id[root])
root = id[root];
while (p != root) { // Do path compression
int next = id[p];
id[p] = root;
p = next;
}
return root;
}
public void union(int p, int q) {
int root1 = find(p);
int root2 = find(q);
if (root1 == root2)
return;
if (sz[root1] < sz[root2]) {
sz[root2] += sz[root1];
id[root1] = root2;
} else {
sz[root1] += sz[root2];
id[root2] = root1;
}
}
public boolean equationsPossible(String[] equations) {
// int n = equations.length;
UnionFind(26);
for (String equation : equations) {
if (equation.charAt(1) == '=') {
int p = equation.charAt(0) - 'a';
int q = equation.charAt(3) - 'a';
union(p, q);
}
}
for (String equation : equations) {
if (equation.charAt(1) == '!') {
int p = equation.charAt(0) - 'a';
int q = equation.charAt(3) - 'a';
if (find(p) == find(q))
return false;
}
}
return true;
}
}
// @lc code=end