Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Satisfiability_of_Equality_Equations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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'];
}
}