File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+
3+ public char [] alphabets ;
4+
5+ public boolean equationsPossible (String [] equations ) {
6+ alphabets = new char [26 ];
7+ for (int i = 0 ; i < 26 ; i ++) {
8+ alphabets [i ] = (char )(i + 'a' );
9+ }
10+ for (int i = 0 ; i < equations .length ; i ++){
11+ String eq = equations [i ];
12+ if (eq .charAt (1 ) == '=' ) {
13+ char p = findCharacter (eq .charAt (0 ));
14+ char q = findCharacter (eq .charAt (3 ));
15+ alphabets [p - 'a' ] = q ;
16+ }
17+ }
18+ for (int i = 0 ; i < equations .length ; i ++){
19+ String eq = equations [i ];
20+ if (eq .charAt (1 ) == '!' ){
21+ char a = findCharacter (eq .charAt (0 ));
22+ char b = findCharacter (eq .charAt (3 ));
23+ if (a == b ) {
24+ return false ;
25+ }
26+ }
27+ }
28+ return true ;
29+ }
30+ public char findCharacter (char ch ){
31+ if (ch != alphabets [ch - 'a' ]) {
32+ alphabets [ch - 'a' ] = findCharacter (alphabets [ch - 'a' ]);
33+ }
34+ return alphabets [ch - 'a' ];
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments