forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSatisfiability of Equality Equations.js
42 lines (42 loc) · 1.17 KB
/
Satisfiability of Equality Equations.js
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
/**
* @param {string[]} equations
* @return {boolean}
*/
class UnionSet {
constructor() {
this.father = new Array(26).fill(0).map((item, index) => index);
}
find(x) {
return this.father[x] = this.father[x] === x ? x : this.find(this.father[x]);
}
merge(a, b) {
const fa = this.find(a);
const fb = this.find(b);
if (fa === fb) return;
this.father[fb] = fa;
}
equal(a, b) {
return this.find(a) === this.find(b);
}
}
var equationsPossible = function(equations) {
const us = new UnionSet();
const base = 'a'.charCodeAt();
// merge, when equal
for(let i = 0; i < equations.length; i++) {
const item = equations[i];
if (item[1] === '!') continue;
const a = item[0].charCodeAt() - base;
const b = item[3].charCodeAt() - base;
us.merge(a, b);
}
// check, when different
for(let i = 0; i < equations.length; i++) {
const item = equations[i];
if (item[1] === '=') continue;
const a = item[0].charCodeAt() - base;
const b = item[3].charCodeAt() - base;
if (us.equal(a, b)) return false;
}
return true;
};