Skip to content

Commit 67760f1

Browse files
authored
Create 990-satisfiability-of-equality-equations.js
1 parent 4d5d17e commit 67760f1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string[]} equations
3+
* @return {boolean}
4+
*/
5+
const equationsPossible = function(equations) {
6+
const uf = new Array(26).fill(0);
7+
const aCode = ('a').charCodeAt(0)
8+
9+
for (let i = 0; i < 26; ++i) uf[i] = i;
10+
for (let e of equations)
11+
if (e.charAt(1) === '=')
12+
uf[find(e.charCodeAt(0) - aCode)] = find(e.charCodeAt(3) - aCode);
13+
for (let e of equations)
14+
if (e.charAt(1) === '!' && find(e.charCodeAt(0) - aCode) === find(e.charCodeAt(3) - aCode))
15+
return false;
16+
return true;
17+
18+
19+
function find(x) {
20+
if (x != uf[x]) uf[x] = find(uf[x]);
21+
return uf[x];
22+
}
23+
};

0 commit comments

Comments
 (0)