File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -21,3 +21,43 @@ const equationsPossible = function(equations) {
21
21
return uf [ x ] ;
22
22
}
23
23
} ;
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
+ } ;
You can’t perform that action at this time.
0 commit comments