Skip to content

Commit 4903be2

Browse files
Merge pull request #78 from susrithasabbini/temp
990. Satisfiability of Equality Equations
2 parents afed902 + a8e3c0c commit 4903be2

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
}

0 commit comments

Comments
 (0)