Skip to content

Commit 30e956a

Browse files
authored
Update 990-satisfiability-of-equality-equations.js
1 parent 67760f1 commit 30e956a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

990-satisfiability-of-equality-equations.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,43 @@ const equationsPossible = function(equations) {
2121
return uf[x];
2222
}
2323
};
24+
25+
26+
// another
27+
28+
/**
29+
* @param {string[]} equations
30+
* @return {boolean}
31+
*/
32+
const equationsPossible = function(equations) {
33+
const num = 26
34+
const visited = new Array(num).fill(false)
35+
const color = new Array(num).fill(-1)
36+
const adj = Array.from(new Array(num), el => [])
37+
const aCode = ('a').charCodeAt(0)
38+
for(let el of equations) {
39+
if(el[1] === '=') {
40+
adj[el[0].charCodeAt(0) - aCode].push(el[3].charCodeAt(0) - aCode)
41+
adj[el[3].charCodeAt(0) - aCode].push(el[0].charCodeAt(0) - aCode)
42+
}
43+
}
44+
let c = 0
45+
for(let i = 0; i < num; i++) {
46+
!visited[i] && dfs(i, c)
47+
c++
48+
}
49+
for(let el of equations) {
50+
if(el[1] === '!' && color[el[0].charCodeAt(0) - aCode] === color[el[3].charCodeAt(0) - aCode]) {
51+
return false
52+
}
53+
}
54+
return true
55+
56+
function dfs(idx, val) {
57+
visited[idx] = true
58+
color[idx] = val
59+
for(let el of adj[idx]) {
60+
!visited[el] && dfs(el, val)
61+
}
62+
}
63+
};

0 commit comments

Comments
 (0)