forked from PawanJaiswal08/leetcode-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSatisfiability_of_Equality_Equations.java
More file actions
36 lines (34 loc) · 1.08 KB
/
Satisfiability_of_Equality_Equations.java
File metadata and controls
36 lines (34 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public char[] alphabets;
public boolean equationsPossible(String[] equations) {
alphabets = new char[26];
for(int i = 0; i < 26; i++) {
alphabets[i] = (char)(i + 'a');
}
for(int i = 0; i < equations.length; i++){
String eq = equations[i];
if(eq.charAt(1) == '=') {
char p = findCharacter(eq.charAt(0));
char q = findCharacter(eq.charAt(3));
alphabets[p - 'a'] = q;
}
}
for(int i = 0; i < equations.length; i++){
String eq = equations[i];
if(eq.charAt(1) == '!'){
char a = findCharacter(eq.charAt(0));
char b = findCharacter(eq.charAt(3));
if(a == b) {
return false;
}
}
}
return true;
}
public char findCharacter(char ch){
if(ch != alphabets[ch - 'a']) {
alphabets[ch - 'a'] = findCharacter(alphabets[ch - 'a']);
}
return alphabets[ch - 'a'];
}
}