Skip to content

Commit da0b1b3

Browse files
committed
Add solution and test-cases for problem 990
1 parent 31756cd commit da0b1b3

File tree

3 files changed

+64
-22
lines changed

3 files changed

+64
-22
lines changed

leetcode/901-1000/0990.Satisfiability-of-Equality-Equations/README.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
# [990.Satisfiability of Equality Equations][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
5+
6+
Return `true` if it is possible to assign integers to variable names so as to satisfy all the given equations, or `false` otherwise.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: equations = ["a==b","b!=a"]
12+
Output: false
13+
Explanation: If we assign say, a = 1 and b = 1, then the first equation is satisfied, but not the second.
14+
There is no way to assign the variables to satisfy both equations.
1315
```
1416

15-
## 题意
16-
> ...
17-
18-
## 题解
17+
**Example 2:**
1918

20-
### 思路1
21-
> ...
22-
Satisfiability of Equality Equations
23-
```go
2419
```
25-
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+
```
2624

2725
## 结语
2826

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,50 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type unionFind990 struct {
4+
father [26]int
5+
}
6+
7+
func (u *unionFind990) findFather(x int) int {
8+
if x != u.father[x] {
9+
u.father[x] = u.findFather(u.father[x])
10+
}
11+
return u.father[x]
12+
}
13+
func (u *unionFind990) union(x, y int) {
14+
fx := u.findFather(x)
15+
fy := u.findFather(y)
16+
if fx < fy {
17+
u.father[fx] = fy
18+
} else {
19+
u.father[fy] = fx
20+
}
21+
}
22+
23+
func Solution(equations []string) bool {
24+
uf := unionFind990{father: [26]int{}}
25+
for i := range 26 {
26+
uf.father[i] = i
27+
}
28+
29+
for _, v := range equations {
30+
eq := v[1:3]
31+
a, b := int(v[0]-'a'), int(v[3]-'a')
32+
if eq != "==" {
33+
continue
34+
}
35+
uf.union(a, b)
36+
}
37+
for _, v := range equations {
38+
eq := v[1:3]
39+
a, b := int(v[0]-'a'), int(v[3]-'a')
40+
if eq == "==" {
41+
continue
42+
}
43+
fa := uf.findFather(a)
44+
fb := uf.findFather(b)
45+
if fa == fb {
46+
return false
47+
}
48+
}
49+
return true
550
}

leetcode/901-1000/0990.Satisfiability-of-Equality-Equations/Solution_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs []string
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []string{"a==b", "b!=a"}, false},
17+
{"TestCase2", []string{"b==a", "a==b"}, true},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)