Boolean Parenthesization Problem
Pattern: Partition and interval DP
Count the ways to bracket T/F symbols joined by &, | and ^ so the result is true. For each segment keep two counts, true and false; for each last operator, combine the left and right counts by its truth table.
No verified practice link yet. The guide below states the problem in full, so you can still solve it here.
The problem
Given a string of symbols T and F joined by operators &, | and ^, return the number of ways to fully bracket it so that it evaluates to true, modulo 1003.
Think first
- Why is counting only true ways not enough?
- How does & combine two sides?
- And ^?
Hints
Brute force, and why it fails
Generate every bracketing: Catalan-many.
The key insight
Each bracketing has a last operator, and the two sides' true and false counts combine independently.
Dry run
'T|T&F^T'. Brackets apply the last operator in different places.
Step 1 of 2
The plan
T[i][i], F[i][i] from the symbol
for each segment and last operator k: combine (T, F) of the left and the right by the truth table
Complexity
- Time
O(n³)- Space
O(n²)
n is the number of symbols.
Edge cases
- One symbol.
- All T with &.
- Long strings: modulo.
I've built my logic, show the code
Java
class Solution {
public int countWays(String s) {
final int MOD = 1003;
int n = (s.length() + 1) / 2; // symbols at even positions, operators at odd ones
int[][] T = new int[n][n], F = new int[n][n]; // ways for symbols i..j to be true / false
for (int i = 0; i < n; i++) { T[i][i] = s.charAt(2 * i) == 'T' ? 1 : 0; F[i][i] = 1 - T[i][i]; }
for (int len = 2; len <= n; len++)
for (int i = 0; i + len <= n; i++) {
int j = i + len - 1;
for (int k = i; k < j; k++) { // the operator after symbol k is applied last
char op = s.charAt(2 * k + 1);
long lt = T[i][k], lf = F[i][k], rt = T[k + 1][j], rf = F[k + 1][j];
long t = op == '&' ? lt * rt : op == '|' ? lt * rt + lt * rf + lf * rt : lt * rf + lf * rt;
long all = (lt + lf) * (rt + rf);
T[i][j] = (int) ((T[i][j] + t) % MOD);
F[i][j] = (int) ((F[i][j] + all - t) % MOD);
}
}
return T[0][n - 1];
}
}C++
class Solution {
public:
int countWays(string s) {
const int MOD = 1003;
int n = (static_cast<int>(s.size()) + 1) / 2; // symbols at even positions, operators at odd ones
vector<vector<long long>> T(n, vector<long long>(n, 0)), F(n, vector<long long>(n, 0)); // true / false ways
for (int i = 0; i < n; i++) { T[i][i] = s[2 * i] == 'T' ? 1 : 0; F[i][i] = 1 - T[i][i]; }
for (int len = 2; len <= n; len++)
for (int i = 0; i + len <= n; i++) {
int j = i + len - 1;
for (int k = i; k < j; k++) { // the operator after symbol k is applied last
char op = s[2 * k + 1];
long long lt = T[i][k], lf = F[i][k], rt = T[k + 1][j], rf = F[k + 1][j];
long long t = op == '&' ? lt * rt : op == '|' ? lt * rt + lt * rf + lf * rt : lt * rf + lf * rt;
long long all = (lt + lf) * (rt + rf);
T[i][j] = (T[i][j] + t) % MOD;
F[i][j] = ((F[i][j] + all - t) % MOD + MOD) % MOD;
}
}
return static_cast<int>(T[0][n - 1]);
}
};Python
class Solution:
def countWays(self, s: str) -> int:
MOD = 1003
vals, ops = s[0::2], s[1::2] # symbols at even positions, operators at odd ones
n = len(vals)
T = [[0] * n for _ in range(n)] # ways for symbols i..j to be true
F = [[0] * n for _ in range(n)] # and to be false
for i, v in enumerate(vals):
T[i][i], F[i][i] = (1, 0) if v == "T" else (0, 1)
for length in range(2, n + 1):
for i in range(n - length + 1):
j = i + length - 1
for k in range(i, j): # the operator after symbol k is applied last
lt, lf, rt, rf = T[i][k], F[i][k], T[k + 1][j], F[k + 1][j]
op = ops[k]
t = lt * rt if op == "&" else lt * rt + lt * rf + lf * rt if op == "|" else lt * rf + lf * rt
T[i][j] = (T[i][j] + t) % MOD
F[i][j] = (F[i][j] + (lt + lf) * (rt + rf) - t) % MOD
return T[0][n - 1]Common mistakes
- Forgetting false counts.
- Index mix-ups between symbols and operators.