Skip to content

Add solution and test-cases for problem 990 #1219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
# [990.Satisfiability of Equality Equations][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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.

Return `true` if it is possible to assign integers to variable names so as to satisfy all the given equations, or `false` otherwise.

**Example 1:**

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

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Satisfiability of Equality Equations
```go
```

Input: equations = ["b==a","a==b"]
Output: true
Explanation: We could assign a = 1 and b = 1 to satisfy both equations.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,50 @@
package Solution

func Solution(x bool) bool {
return x
type unionFind990 struct {
father [26]int
}

func (u *unionFind990) findFather(x int) int {
if x != u.father[x] {
u.father[x] = u.findFather(u.father[x])
}
return u.father[x]
}
func (u *unionFind990) union(x, y int) {
fx := u.findFather(x)
fy := u.findFather(y)
if fx < fy {
u.father[fx] = fy
} else {
u.father[fy] = fx
}
}

func Solution(equations []string) bool {
uf := unionFind990{father: [26]int{}}
for i := range 26 {
uf.father[i] = i
}

for _, v := range equations {
eq := v[1:3]
a, b := int(v[0]-'a'), int(v[3]-'a')
if eq != "==" {
continue
}
uf.union(a, b)
}
for _, v := range equations {
eq := v[1:3]
a, b := int(v[0]-'a'), int(v[3]-'a')
if eq == "==" {
continue
}
fa := uf.findFather(a)
fb := uf.findFather(b)
if fa == fb {
return false
}
}
return true
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
inputs []string
expect bool
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []string{"a==b", "b!=a"}, false},
{"TestCase2", []string{"b==a", "a==b"}, true},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading