-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_equal.go
45 lines (41 loc) · 1.25 KB
/
check_equal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package problem0990
/*
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.
*/
const OFFSET = 'a'
func findParent(root []int, x int) int {
if x != root[x] {
root[x] = findParent(root, root[x])
}
return root[x]
}
func equationsPossible(equations []string) bool {
var root = make([]int, 26)
var parentLeft, parentRight int
// Making each variable it's own parent
for i := 0; i < 26; i++ {
root[i] = i
}
// Building equalities
for _, eq := range equations {
if eq[1] == '=' {
parentLeft = findParent(root, int(eq[0]-OFFSET))
parentRight = findParent(root, int(eq[3]-OFFSET))
root[parentLeft] = parentRight
}
}
// Checking inequalities
for _, eq := range equations {
if eq[1] == '!' {
parentLeft = findParent(root, int(eq[0]-OFFSET))
parentRight = findParent(root, int(eq[3]-OFFSET))
if parentLeft == parentRight {
return false
}
}
}
return true
}