|
1 |
| -package heap; |
| 1 | +package depth_first_search; |
| 2 | + |
2 | 3 | import java.util.*;
|
| 4 | + |
3 | 5 | /**
|
4 | 6 | * Created by gouthamvidyapradhan on 25/07/2019 Given an array equations of strings that represent
|
5 | 7 | * relationships between variables, each string equations[i] has length 4 and takes one of two
|
|
29 | 31 | * <p>1 <= equations.length <= 500 equations[i].length == 4 equations[i][0] and equations[i][3] are
|
30 | 32 | * lowercase letters equations[i][1] is either '=' or '!' equations[i][2] is '='
|
31 | 33 | *
|
32 |
| -* Solution: O(N) For all the equations which are of the form 'a==b' form a graph of connected components. Start |
33 |
| -* assigning values to each of the connected components. All the nodes in the connected components should have the |
34 |
| -* same value assigned - If any of the connected components fails this criteria |
35 |
| -* then return false. |
| 34 | +* <p>Solution: O(N) For all the equations which are of the form 'a==b' form a graph of connected |
| 35 | +* components. Start assigning values to each of the connected components. All the nodes in the |
| 36 | +* connected components should have the same value assigned - If any of the connected components |
| 37 | +* fails this criteria then return false. |
36 | 38 | */
|
37 |
| -public class Task2 { |
| 39 | +public class SatisfiabilityOfEquations { |
38 | 40 | public static void main(String[] args) {
|
39 |
| -String[] input = {"c==c","f!=a","f==b","b==c"}; |
40 |
| -System.out.println(new Task2().equationsPossible(input)); |
| 41 | +String[] input = {"c==c", "f!=a", "f==b", "b==c"}; |
| 42 | +System.out.println(new SatisfiabilityOfEquations().equationsPossible(input)); |
41 | 43 | }
|
42 | 44 |
|
43 |
| -private Set<Character> done; |
44 |
| -private Map<Character, Integer> valueMap; |
45 |
| -private int count = 0; |
46 |
| -public boolean equationsPossible(String[] equations) { |
47 |
| -Map<Character, List<Character>> graph = new HashMap<>(); |
48 |
| -done = new HashSet<>(); |
49 |
| -valueMap = new HashMap<>(); |
50 |
| -for(String eq : equations){ |
51 |
| -if(eq.charAt(1) == '='){ |
52 |
| -graph.putIfAbsent(eq.charAt(0), new ArrayList<>()); |
53 |
| -graph.get(eq.charAt(0)).add(eq.charAt(3)); |
54 |
| -graph.putIfAbsent(eq.charAt(3), new ArrayList<>()); |
55 |
| -graph.get(eq.charAt(3)).add(eq.charAt(0)); |
| 45 | +private Set<Character> done; |
| 46 | +private Map<Character, Integer> valueMap; |
| 47 | +private int count = 0; |
56 | 48 |
|
57 |
| -} |
58 |
| -} |
59 |
| -for(char c : graph.keySet()){ |
60 |
| -if(!done.contains(c)){ |
61 |
| -dfs(c, graph, ++count); |
62 |
| -} |
63 |
| -} |
| 49 | +public boolean equationsPossible(String[] equations) { |
| 50 | +Map<Character, List<Character>> graph = new HashMap<>(); |
| 51 | +done = new HashSet<>(); |
| 52 | +valueMap = new HashMap<>(); |
| 53 | +for (String eq : equations) { |
| 54 | +if (eq.charAt(1) == '=') { |
| 55 | +graph.putIfAbsent(eq.charAt(0), new ArrayList<>()); |
| 56 | +graph.get(eq.charAt(0)).add(eq.charAt(3)); |
| 57 | +graph.putIfAbsent(eq.charAt(3), new ArrayList<>()); |
| 58 | +graph.get(eq.charAt(3)).add(eq.charAt(0)); |
| 59 | +} |
| 60 | +} |
| 61 | +for (char c : graph.keySet()) { |
| 62 | +if (!done.contains(c)) { |
| 63 | +dfs(c, graph, ++count); |
| 64 | +} |
| 65 | +} |
64 | 66 |
|
65 |
| -for(String eq : equations){ |
66 |
| -if(eq.charAt(1) == '!'){ |
67 |
| -char a = eq.charAt(0); |
68 |
| -char b = eq.charAt(3); |
69 |
| -if(a == b) return false; |
70 |
| -if(valueMap.containsKey(a) && valueMap.containsKey(b)){ |
71 |
| -if(valueMap.get(a).intValue() == valueMap.get(b).intValue()){ |
72 |
| -return false; |
73 |
| -} |
74 |
| -} |
75 |
| -} |
| 67 | +for (String eq : equations) { |
| 68 | +if (eq.charAt(1) == '!') { |
| 69 | +char a = eq.charAt(0); |
| 70 | +char b = eq.charAt(3); |
| 71 | +if (a == b) return false; |
| 72 | +if (valueMap.containsKey(a) && valueMap.containsKey(b)) { |
| 73 | +if (valueMap.get(a).intValue() == valueMap.get(b).intValue()) { |
| 74 | +return false; |
| 75 | +} |
76 | 76 | }
|
77 |
| -return true; |
| 77 | +} |
78 | 78 | }
|
| 79 | +return true; |
| 80 | +} |
79 | 81 |
|
80 |
| - |
81 |
| -private boolean dfs(char node, Map<Character, List<Character>> graph, int value){ |
82 |
| -done.add(node); |
83 |
| -valueMap.put(node, value); |
84 |
| -List<Character> children = graph.get(node); |
85 |
| -if(!children.isEmpty()){ |
86 |
| -for(char c : children){ |
87 |
| -if(!done.contains(c)){ |
88 |
| -boolean status = dfs(c, graph, value); |
89 |
| -if(!status) { |
90 |
| -return status; |
91 |
| -} |
92 |
| -} else{ |
93 |
| -if(valueMap.get(c) != value){ |
94 |
| -return false; |
95 |
| -} |
96 |
| -} |
97 |
| -} |
| 82 | +private boolean dfs(char node, Map<Character, List<Character>> graph, int value) { |
| 83 | +done.add(node); |
| 84 | +valueMap.put(node, value); |
| 85 | +List<Character> children = graph.get(node); |
| 86 | +if (!children.isEmpty()) { |
| 87 | +for (char c : children) { |
| 88 | +if (!done.contains(c)) { |
| 89 | +boolean status = dfs(c, graph, value); |
| 90 | +if (!status) { |
| 91 | +return status; |
| 92 | +} |
| 93 | +} else { |
| 94 | +if (valueMap.get(c) != value) { |
| 95 | +return false; |
| 96 | +} |
98 | 97 | }
|
99 |
| -return true; |
| 98 | +} |
100 | 99 | }
|
101 |
| - |
| 100 | +return true; |
| 101 | +} |
102 | 102 | }
|
0 commit comments