|
| 1 | +/* |
| 2 | +
|
| 3 | + -* Satisfiability Of Equality Equations *- |
| 4 | +
|
| 5 | +
|
| 6 | +You are given an array of strings equations that represent relationships between variables where each string equations[i] is of length 4 and takes one of two different forms: "xi==yi" or "xi!=yi".Here, xi and yi are lowercase letters (not necessarily different) that represent one-letter variable names. |
| 7 | +
|
| 8 | +Return true if it is possible to assign integers to variable names so as to satisfy all the given equations, or false otherwise. |
| 9 | +
|
| 10 | +
|
| 11 | +
|
| 12 | +Example 1: |
| 13 | +
|
| 14 | +Input: equations = ["a==b","b!=a"] |
| 15 | +Output: false |
| 16 | +Explanation: If we assign say, a = 1 and b = 1, then the first equation is satisfied, but not the second. |
| 17 | +There is no way to assign the variables to satisfy both equations. |
| 18 | +Example 2: |
| 19 | +
|
| 20 | +Input: equations = ["b==a","a==b"] |
| 21 | +Output: true |
| 22 | +Explanation: We could assign a = 1 and b = 1 to satisfy both equations. |
| 23 | +
|
| 24 | +
|
| 25 | +Constraints: |
| 26 | +
|
| 27 | +1 <= equations.length <= 500 |
| 28 | +equations[i].length == 4 |
| 29 | +equations[i][0] is a lowercase letter. |
| 30 | +equations[i][1] is either '=' or '!'. |
| 31 | +equations[i][2] is '='. |
| 32 | +equations[i][3] is a lowercase letter. |
| 33 | +
|
| 34 | +
|
| 35 | +*/ |
| 36 | + |
| 37 | +class A { |
| 38 | +// Runtime: 487 ms, faster than 100.00% of Dart online submissions for Satisfiability of Equality Equations. |
| 39 | +// Memory Usage: 142.4 MB, less than 100.00% of Dart online submissions for Satisfiability of Equality Equations. |
| 40 | + bool equationsPossible(List<String> equations) { |
| 41 | + String getParent(String? x, Map<String, String> m) { |
| 42 | + if (m[x] == x) return x!; |
| 43 | + return m[x!] = getParent(m[x], m); |
| 44 | + } |
| 45 | + |
| 46 | + List<int> equal = List.empty(growable: true); |
| 47 | + List<int> notEqual = List.empty(growable: true); |
| 48 | + Map<String, String> parent = {}; |
| 49 | + int n = equations.length; |
| 50 | + for (int i = 0; i < n; i++) { |
| 51 | + parent[equations[i][0]] = equations[i][0]; |
| 52 | + parent[equations[i][3]] = equations[i][3]; |
| 53 | + if (equations[i][1] == '=') { |
| 54 | + equal.add(i); |
| 55 | + } else { |
| 56 | + notEqual.add(i); |
| 57 | + } |
| 58 | + } |
| 59 | + for (int i = 0; i < equal.length; i++) { |
| 60 | + int idx = equal[i]; |
| 61 | + String u = equations[idx][0]; |
| 62 | + String v = equations[idx][3]; |
| 63 | + parent[getParent(u, parent)] = parent[getParent(v, parent)]!; |
| 64 | + } |
| 65 | + for (int i = 0; i < notEqual.length; i++) { |
| 66 | + int idx = notEqual[i]; |
| 67 | + String u = equations[idx][0]; |
| 68 | + String v = equations[idx][3]; |
| 69 | + if (getParent(u, parent) == getParent(v, parent)) return false; |
| 70 | + } |
| 71 | + return true; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// extension CharAt<T> on String { |
| 76 | +// String charAt(String subject, int position) { |
| 77 | +// if (subject is! String || |
| 78 | +// subject.length <= position || |
| 79 | +// subject.length + position < 0) { |
| 80 | +// return ''; |
| 81 | +// } |
| 82 | + |
| 83 | +// int _realPosition = position < 0 ? subject.length + position : position; |
| 84 | + |
| 85 | +// return subject[_realPosition]; |
| 86 | +// } |
| 87 | +// } |
| 88 | + |
| 89 | +// extension Operator on String { |
| 90 | +// operator -(String other) { |
| 91 | +// return double.parse(this) - double.parse(other); |
| 92 | +// } |
| 93 | +// } |
| 94 | + |
| 95 | +// class B { |
| 96 | +// // 256 |
| 97 | +// List<int> parent = List.filled(26, 0); |
| 98 | +// int find(int c) { |
| 99 | +// return parent[c] == c ? c : find(parent[c]); |
| 100 | +// } |
| 101 | + |
| 102 | +// bool equationsPossible(List<String> equations) { |
| 103 | +// for (int i = 0; i < 26; i++) { |
| 104 | +// parent[i] = i; |
| 105 | +// } |
| 106 | +// for (String s in equations) { |
| 107 | +// var c1 = s[0]; |
| 108 | +// var c2 = s[3]; |
| 109 | +// if (s[1] == '=') { |
| 110 | +// int i1 = find(c1 - 'a'); |
| 111 | +// int i2 = find(c2 - 'a'); |
| 112 | +// if (i1 != i2) { |
| 113 | +// parent[i1] = i2; |
| 114 | +// } |
| 115 | +// } else if (c1 == c2) { |
| 116 | +// return false; |
| 117 | +// } |
| 118 | +// } |
| 119 | +// for (String s in equations) { |
| 120 | +// if (s[1] == '!') { |
| 121 | +// int x = find(s[0] - 'a'); |
| 122 | +// int y = find(s[3] - 'a'); |
| 123 | +// if (x == y) { |
| 124 | +// return false; |
| 125 | +// } |
| 126 | +// } |
| 127 | +// } |
| 128 | +// return true; |
| 129 | +// } |
| 130 | +// } |
| 131 | + |
| 132 | +// class C { |
| 133 | +// bool equationsPossible(List<String> equations) {} |
| 134 | +// } |
| 135 | + |
| 136 | +// class D { |
| 137 | +// bool equationsPossible(List<String> equations) {} |
| 138 | +// } |
| 139 | + |
| 140 | +// class E { |
| 141 | +// bool equationsPossible(List<String> equations) {} |
| 142 | +// } |
0 commit comments