diff --git a/leetcode/901-1000/0990.Satisfiability-of-Equality-Equations/README.md b/leetcode/901-1000/0990.Satisfiability-of-Equality-Equations/README.md index 4bf7bb93a..5062995f2 100644 --- a/leetcode/901-1000/0990.Satisfiability-of-Equality-Equations/README.md +++ b/leetcode/901-1000/0990.Satisfiability-of-Equality-Equations/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/901-1000/0990.Satisfiability-of-Equality-Equations/Solution.go b/leetcode/901-1000/0990.Satisfiability-of-Equality-Equations/Solution.go index d115ccf5e..f87141465 100644 --- a/leetcode/901-1000/0990.Satisfiability-of-Equality-Equations/Solution.go +++ b/leetcode/901-1000/0990.Satisfiability-of-Equality-Equations/Solution.go @@ -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 } diff --git a/leetcode/901-1000/0990.Satisfiability-of-Equality-Equations/Solution_test.go b/leetcode/901-1000/0990.Satisfiability-of-Equality-Equations/Solution_test.go index 14ff50eb4..dded36010 100644 --- a/leetcode/901-1000/0990.Satisfiability-of-Equality-Equations/Solution_test.go +++ b/leetcode/901-1000/0990.Satisfiability-of-Equality-Equations/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }