forked from dylansun/leetcode-cn-scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo990.scala
49 lines (44 loc) · 1.18 KB
/
No990.scala
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
46
47
48
49
/**
* Created by lilisun on 3/3/19.
*/
object No990 {
def equationsPossible(equations: Array[String]): Boolean = {
val eq = equations.filter(x => x(1) == '=')
val neq = equations.filter(x => x(1) == '!')
var e = Set[Set[Char]]()
for(x <- 'a' to 'z') e = e + Set(x)
for(equation <- eq){
val a = e.filter(_.contains(equation(0)))
val b = e.filter(_.contains(equation(3)))
e = e -- a
e = e -- b
val c = a.flatten ++ b.flatten
e = e + c
}
for(equation <- neq){
val a = e.filter(_.contains(equation(0)))
val b = e.filter(_.contains(equation(3)))
if(a == b) return false
}
true
}
def main(args: Array[String]): Unit = {
var e = Set[Set[Char]]()
for(x <- 'a' to 'z') e = e + Set(x)
println(e.size)
// a== b
val a = e.filter( x => x.contains('a'))
val b = e.filter( x => x.contains('b'))
e = e -- a
e = e -- b
println(e.size)
val c = a.flatten ++ b.flatten
println(c.size)
e = e + c
println(e.size)
println(e)
println("Test")
val equations = Array("c==c","b==d","x!=z","x==b", "z==c", "c==d")
println(equationsPossible(equations))
}
}