mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-25 20:21:47 +08:00
27 lines
509 B
Go
27 lines
509 B
Go
package leetcode
|
|
|
|
import (
|
|
"github.com/halfrost/LeetCode-Go/template"
|
|
)
|
|
|
|
func equationsPossible(equations []string) bool {
|
|
if len(equations) == 0 {
|
|
return false
|
|
}
|
|
uf := template.UnionFind{}
|
|
uf.Init(26)
|
|
for _, equ := range equations {
|
|
if equ[1] == '=' && equ[2] == '=' {
|
|
uf.Union(int(equ[0]-'a'), int(equ[3]-'a'))
|
|
}
|
|
}
|
|
for _, equ := range equations {
|
|
if equ[1] == '!' && equ[2] == '=' {
|
|
if uf.Find(int(equ[0]-'a')) == uf.Find(int(equ[3]-'a')) {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|