|
| 1 | +package heap; |
| 2 | +import java.util.*; |
| 3 | +/** |
| 4 | + * Created by gouthamvidyapradhan on 25/07/2019 Given an array equations of strings that represent |
| 5 | + * relationships between variables, each string equations[i] has length 4 and takes one of two |
| 6 | + * different forms: "a==b" or "a!=b". Here, a and b are lowercase letters (not necessarily |
| 7 | + * different) that represent one-letter variable names. |
| 8 | + * |
| 9 | + * <p>Return true if and only if it is possible to assign integers to variable names so as to |
| 10 | + * satisfy all the given equations. |
| 11 | + * |
| 12 | + * <p>Example 1: |
| 13 | + * |
| 14 | + * <p>Input: ["a==b","b!=a"] Output: false Explanation: If we assign say, a = 1 and b = 1, then the |
| 15 | + * first equation is satisfied, but not the second. There is no way to assign the variables to |
| 16 | + * satisfy both equations. Example 2: |
| 17 | + * |
| 18 | + * <p>Input: ["b==a","a==b"] Output: true Explanation: We could assign a = 1 and b = 1 to satisfy |
| 19 | + * both equations. Example 3: |
| 20 | + * |
| 21 | + * <p>Input: ["a==b","b==c","a==c"] Output: true Example 4: |
| 22 | + * |
| 23 | + * <p>Input: ["a==b","b!=c","c==a"] Output: false Example 5: |
| 24 | + * |
| 25 | + * <p>Input: ["c==c","b==d","x!=z"] Output: true |
| 26 | + * |
| 27 | + * <p>Note: |
| 28 | + * |
| 29 | + * <p>1 <= equations.length <= 500 equations[i].length == 4 equations[i][0] and equations[i][3] are |
| 30 | + * lowercase letters equations[i][1] is either '=' or '!' equations[i][2] is '=' |
| 31 | + * |
| 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. |
| 36 | + */ |
| 37 | +public class Task2 { |
| 38 | + 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 | + } |
| 42 | + |
| 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)); |
| 56 | + |
| 57 | + } |
| 58 | + } |
| 59 | + for(char c : graph.keySet()){ |
| 60 | + if(!done.contains(c)){ |
| 61 | + dfs(c, graph, ++count); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 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 | + } |
| 76 | + } |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 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 | + } |
| 98 | + } |
| 99 | + return true; |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments