MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / Solution

Class Solution

SatisfiabilityOfEqualityEquations.java:1–24  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Solution {
2 public boolean equationsPossible(String[] equations) {
3 DisjointSet dsu = new DisjointSet(26);
4 ArrayList<int[]> notEqualEdges = new ArrayList<>();
5 for(String equation : equations){
6 int u = equation.charAt(0) - 'a';
7 int v = equation.charAt(3) - 'a';
8 //construct graphs using "equal to" equations
9 if(equation.charAt(1)== '='){
10 dsu.unionBySize(u,v);
11 }else{ //store "not equal" equations
12 notEqualEdges.add(new int[]{u,v});
13 }
14 }
15 for(int edge[] : notEqualEdges){
16 int u = edge[0];
17 int v = edge[1];
18 if(dsu.findRootParent(u) == dsu.findRootParent(v)){
19 return false;
20 }
21 }
22 return true;
23 }
24}
25
26
27

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected