|
| 1 | +package chapter08RecursionAndDynamicProgramming; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | + |
| 5 | +/** |
| 6 | + * |
| 7 | + * Problem: Given a boolean expression consisting of the symbols 0 (false), |
| 8 | + * 1(true), |
| 9 | + * |
| 10 | + */ |
| 11 | +public class BooleanEvaluation { |
| 12 | + |
| 13 | + /** |
| 14 | + * Method 1: Brute force |
| 15 | + */ |
| 16 | + public static int count = 0; |
| 17 | + |
| 18 | + public static boolean stringToBool(String c) { |
| 19 | + return c.equals("1") ? true : false; |
| 20 | + } |
| 21 | + |
| 22 | + public static int countEval1(String s, boolean result) { |
| 23 | + count++; |
| 24 | + if (s.length() == 0) { |
| 25 | + return 0; |
| 26 | + } |
| 27 | + if (s.length() == 1) { |
| 28 | + return stringToBool(s) == result ? 1 : 0; |
| 29 | + } |
| 30 | + int ways = 0; |
| 31 | + for (int i = 1; i < s.length(); i += 2) { |
| 32 | + char c = s.charAt(i); |
| 33 | + String left = s.substring(0, i); |
| 34 | + String right = s.substring(i + 1, s.length()); |
| 35 | + int leftTrue = countEval1(left, true); |
| 36 | + int leftFalse = countEval1(left, false); |
| 37 | + int rightTrue = countEval1(right, true); |
| 38 | + int rightFalse = countEval1(right, false); |
| 39 | + int total = (leftTrue + leftFalse) * (rightTrue + rightFalse); |
| 40 | + int totalTrue = 0; |
| 41 | + if (c == '^') { // required: one true and one false |
| 42 | + totalTrue = leftTrue * rightFalse + leftFalse * rightTrue; |
| 43 | + } else if (c == '&') { // required: both true |
| 44 | + totalTrue = leftTrue * rightTrue; |
| 45 | + } else if (c == '|') { // required: anything but both false |
| 46 | + totalTrue = leftTrue * rightTrue + leftFalse * rightTrue + leftTrue * rightFalse; |
| 47 | + } |
| 48 | + int subWays = result ? totalTrue : total - totalTrue; |
| 49 | + ways += subWays; |
| 50 | + } |
| 51 | + return ways; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Method 2: Memorization |
| 56 | + */ |
| 57 | + public static int countEval2(String s, boolean result) { |
| 58 | + return countEval2(s, result, new HashMap<String, Integer>()); |
| 59 | + } |
| 60 | + |
| 61 | + public static int countEval2(String s, boolean result, HashMap<String, Integer> memo) { |
| 62 | + count++; |
| 63 | + if (s.length() == 0) { |
| 64 | + return 0; |
| 65 | + } |
| 66 | + if (s.length() == 1) { |
| 67 | + return stringToBool(s) == result ? 1 : 0; |
| 68 | + } |
| 69 | + if (memo.containsKey(result + s)) { |
| 70 | + return memo.get(result + s); |
| 71 | + } |
| 72 | + int ways = 0; |
| 73 | + for (int i = 1; i < s.length(); i += 2) { |
| 74 | + char c = s.charAt(i); |
| 75 | + String left = s.substring(0, i); |
| 76 | + String right = s.substring(i + 1, s.length()); |
| 77 | + int leftTrue = countEval2(left, true, memo); |
| 78 | + int leftFalse = countEval2(left, false, memo); |
| 79 | + int rightTrue = countEval2(right, true, memo); |
| 80 | + int rightFalse = countEval2(right, false, memo); |
| 81 | + int total = (leftTrue + leftFalse) * (rightTrue + rightFalse); |
| 82 | + int totalTrue = 0; |
| 83 | + if (c == '^') { |
| 84 | + totalTrue = leftTrue * rightFalse + leftFalse * rightTrue; |
| 85 | + } else if (c == '&') { |
| 86 | + totalTrue = leftTrue * rightTrue; |
| 87 | + } else if (c == '|') { |
| 88 | + totalTrue = leftTrue * rightTrue + leftFalse * rightTrue + leftTrue * rightFalse; |
| 89 | + } |
| 90 | + int subWays = result ? totalTrue : total - totalTrue; |
| 91 | + ways += subWays; |
| 92 | + } |
| 93 | + memo.put(result + s, ways); |
| 94 | + return ways; |
| 95 | + } |
| 96 | +} |
0 commit comments