forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBooleanEvaluation.java
41 lines (35 loc) · 1.36 KB
/
BooleanEvaluation.java
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
package dynamic;
public class BooleanEvaluation {
public static void main(String[] args) {
String s = "1^0|0|1";
System.out.println(countEval(s, false));
}
private static int countEval(String s, boolean result) {
if (s.length() == 0) return 0;
if (s.length() == 1) {
return s.equals("1") == result ? 1 : 0;
}
int ways = 0;
for (int i = 1; i < s.length(); i = i + 2) {
char operator = s.charAt(i);
String left = s.substring(0, i);
String right = s.substring(i + 1);
int leftFalse = countEval(left, false);
int rightFalse = countEval(right, false);
int leftTrue = countEval(left, true);
int rightTrue = countEval(right, true);
int total = (leftTrue + leftFalse) * (rightFalse + rightTrue);
int totalTrue = 0;
if (operator == '^') {
totalTrue = leftTrue * rightFalse + leftFalse * rightTrue;
} else if (operator == '&') {
totalTrue = leftTrue * rightTrue;
} else if (operator == '|') {
totalTrue = leftTrue * rightTrue + leftFalse * rightTrue + leftTrue * rightFalse;
}
int subways = result ? totalTrue : total - totalTrue;
ways += subways;
}
return ways;
}
}