counting boolean parenthesizations implementation
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The boolean parenthesization problem asks how many ways a boolean expression can be parenthesized so that it evaluates to True. A brute-force recursive solution quickly explodes because the same subexpressions are recomputed many times. The standard implementation uses dynamic programming to count true and false outcomes for every subrange of the expression.
Problem Setup
The input is usually split into two strings:
- symbols such as
TFT - operators such as
^&
For example, the expression T ^ F & T can be represented as:
The goal is to count parenthesizations that make the full expression True, not merely to evaluate it once.
Dynamic Programming State
For each subexpression from index i to j, store two values:
- '
true_dp[i][j]: number of ways it evaluates toTrue' - '
false_dp[i][j]: number of ways it evaluates toFalse'
The base case is a single symbol:
- '
Tgives one true way and zero false ways' - '
Fgives zero true ways and one false way'
Then expand to longer ranges by picking every possible split point k and combining left and right counts according to the operator at k.
Python Implementation
This implementation runs in O(n^3) time because it considers every range and every split point inside that range. The tables take O(n^2) space.
Why the Transition Works
The important idea is that each operator combines counts, not raw boolean values.
If the operator is &, the combined expression is true only when both sides are true. If the operator is |, the combined expression is false only when both sides are false. If the operator is ^, the combined expression is true when the sides differ.
That is why the DP stores both true and false counts. You need both to compute the next range correctly.
Recursive Version Versus DP
A naive recursive implementation mirrors the mathematical definition nicely, but it repeats the same subproblems again and again. Dynamic programming removes that waste by storing answers for subranges once.
For interview and contest settings, memoized recursion is often a good intermediate step. For production-quality clarity and predictable complexity, the bottom-up DP is usually easier to reason about.
Common Pitfalls
- Forgetting to track false counts as well as true counts.
- Misaligning operator indexes with symbol indexes.
- Treating the operators as normal precedence rules. In this problem, every legal parenthesization counts.
- Writing the recursion first and never memoizing it, which causes exponential runtime.
- Returning a boolean instead of the number of valid parenthesizations.
Summary
- Boolean parenthesization is a counting problem, not just an evaluation problem.
- The standard solution stores true and false counts for every subexpression.
- Each operator has its own count-combination rule.
- A bottom-up DP runs in
O(n^3)time andO(n^2)space. - Most bugs come from index mistakes or from forgetting that false counts are part of the state.
Related reading
- Counting inversions in an array
- Counting inversions in ranges
- Counting Inversions using BIT
- Counting minimum number of swaps to group characters in string
- Counting number of bits How does this line work ? nnn-1;
- Counting palindromic substrings in On
- Counting the adjacent swaps required to convert one permutation into another
- Counting the bits set in the Fibonacci number system?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.