Skip to content

Commit 86cba18

Browse files
committed
leetcode
1 parent 7059302 commit 86cba18

File tree

4 files changed

+310
-1
lines changed

4 files changed

+310
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ This repo contain leetcode solution using DART and GO programming language. Most
4545
- [Concatenation of Consecutive Binary Numbers](ConcatenationofConsecutiveBinaryNumbers/concatenation_of_consecutive_binary_numbers.dart)
4646
- [Path Sum II](PathSumII/path_sum_II.dart)
4747
- [Pascal's Triangle II](Pascal'sTriangle-II/pascals_riangle_II.dart)
48-
4948
- [Design Circular Queue](DesignCircularQueue/design_circular_queue.dart)
49+
- [Satisfiability of Equality Equations](SatisfiabilityOfEqualityEquations/satisfiability_of_equality_equations.dart)
5050

5151
## Reach me via
5252

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
// }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package main
2+
3+
// find the root of node x.
4+
// here we are not using parent[x],
5+
// because it may not contain the updated value of the connected component that x belongs to.
6+
// therefore, we walk the ancestors of the vertex until we reach the root.
7+
// func find(parent []int, x int) int {
8+
// if parent[x] == x {
9+
// return x
10+
// }
11+
// return find(parent, parent[x])
12+
// }
13+
14+
// // the idea is to put all characters in the same group if they are equal
15+
// // in order to do that, we can use Disjoint Set Union (dsu) aka Union Find
16+
// // for dsu tutorial, please check out https://wingkwong.github.io/leetcode-the-hard-way/tutorials/graph-theory/disjoint-set-union
17+
// func equationsPossible(equations []string) bool {
18+
// // at the beginning, put each character index in its own group
19+
// // so we will have 26 groups with one character each
20+
// // i.e. 'a' in group 0, 'b' in group 1, ..., 'z' in group 25
21+
// parent := make([]int, 26)
22+
// for i := range parent {
23+
// parent[i] = i
24+
// }
25+
// for _, e := range equations {
26+
// if e[1] == '=' {
27+
// // e.g. a == b
28+
// // then we group them together
29+
// // how? we use `find` function to find out the parent group of the target character index
30+
// // then update parent. a & b would be in group 1 (i.e. a merged into the group where b belongs to)
31+
// // or you can also do `find(parent, int(e[3] - 'a')) = find(parent, int(e[0] - 'a'))`
32+
// // i.e. b merged into the group where a belongs to
33+
// parent[find(parent, int(e[0]-'a'))] = find(parent, int(e[3]-'a'))
34+
// }
35+
// }
36+
// // handle != case
37+
// for _, e := range equations {
38+
// // if two characters are not equal
39+
// // then which means their parent must not be equal
40+
// if e[1] == '!' && find(parent, int(e[0]-'a')) == find(parent, int(e[3]-'a')) {
41+
// return false
42+
// }
43+
// }
44+
// return true
45+
// }
46+
func equationsPossible(equations []string) bool {
47+
equals := map[byte]map[byte]bool{}
48+
not_equals := [][]byte{}
49+
for _, eq := range equations {
50+
if string(eq[1]) == "=" {
51+
if _, ok := equals[eq[0]]; !ok {
52+
equals[eq[0]] = map[byte]bool{}
53+
}
54+
equals[eq[0]][eq[3]] = true
55+
if _, ok := equals[eq[3]]; !ok {
56+
equals[eq[3]] = map[byte]bool{}
57+
}
58+
equals[eq[3]][eq[0]] = true
59+
} else {
60+
not_equals = append(not_equals, []byte{eq[3], eq[0]})
61+
}
62+
}
63+
64+
for key := range equals {
65+
dfs(key, key, map[byte]bool{}, &equals)
66+
}
67+
68+
for _, pair := range not_equals {
69+
node1 := pair[0]
70+
node2 := pair[1]
71+
if node1 == node2 {
72+
return false
73+
}
74+
if v, ok := equals[node1]; ok {
75+
if _, ok := v[node2]; ok {
76+
return false
77+
}
78+
}
79+
}
80+
return true
81+
}
82+
83+
func dfs(start, curr byte, visited map[byte]bool, equals *map[byte]map[byte]bool) {
84+
if _, ok := visited[curr]; ok {
85+
return
86+
}
87+
visited[curr] = true
88+
(*equals)[start][curr] = true
89+
for v := range (*equals)[curr] {
90+
dfs(start, v, visited, equals)
91+
}
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# ✅ DART || simple fast easy || with explanation
2+
3+
* To solve this, we will follow these steps −
4+
5+
* Define a method called getParent(), this will take character x and the map m, this will work as follows −
6+
7+
* if m[x] = x, then return x,
8+
9+
* otherwise set m[x] := getParent(m[x], m) and return m[x]
10+
11+
* From the main method, do the following −
12+
13+
* define two arrays equal and notEqual, create a map called parent
14+
15+
* n := size of e
16+
17+
* for i in range 0 to n – 1
18+
19+
* set parent[e[i, 0]] := e[i, 0], parent[e[i, 3]] := e[i, 3]
20+
21+
* if e[i, 1] is equal sign, then insert i into equal array, otherwise insert i into notEqual array
22+
23+
* for i in range 0 to size of equal – 1
24+
25+
* index := equal[i], set u, v as e[index, 0] and e[index, 3]
26+
27+
* parent[getParent(u, parent)] := parent[getParent(v, parent)]
28+
29+
* for i in range 0 to size of notEqual – 1
30+
31+
* index := notEqual[i], set u, v as e[index, 0] and e[index, 3]
32+
33+
* if getParent(u, parent) = getParent(v, parent), then return false
34+
35+
* return true
36+
37+
```dart
38+
class Solution {
39+
// Runtime: 487 ms, faster than 100.00% of Dart online submissions for Satisfiability of Equality Equations.
40+
// Memory Usage: 142.4 MB, less than 100.00% of Dart online submissions for Satisfiability of Equality Equations.
41+
bool equationsPossible(List<String> equations) {
42+
String getParent(String? x, Map<String, String> m) {
43+
if (m[x] == x) return x!;
44+
return m[x!] = getParent(m[x], m);
45+
}
46+
47+
List<int> equal = List.empty(growable: true);
48+
List<int> notEqual = List.empty(growable: true);
49+
Map<String, String> parent = {};
50+
int n = equations.length;
51+
for (int i = 0; i < n; i++) {
52+
parent[equations[i][0]] = equations[i][0];
53+
parent[equations[i][3]] = equations[i][3];
54+
if (equations[i][1] == '=') {
55+
equal.add(i);
56+
} else {
57+
notEqual.add(i);
58+
}
59+
}
60+
for (int i = 0; i < equal.length; i++) {
61+
int idx = equal[i];
62+
String u = equations[idx][0];
63+
String v = equations[idx][3];
64+
parent[getParent(u, parent)] = parent[getParent(v, parent)]!;
65+
}
66+
for (int i = 0; i < notEqual.length; i++) {
67+
int idx = notEqual[i];
68+
String u = equations[idx][0];
69+
String v = equations[idx][3];
70+
if (getParent(u, parent) == getParent(v, parent)) return false;
71+
}
72+
return true;
73+
}
74+
}
75+
```

0 commit comments

Comments
 (0)