Parenthesizing a string so that expression takes a given value
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
Given an expression string such as 2*3+5, the parenthesization problem asks whether some placement of parentheses makes the expression evaluate to a target value. This is a classic dynamic-programming problem because the same subexpressions appear repeatedly. The standard solution computes all possible values for every substring and then checks whether the target is among them.
Why Parentheses Change the Value
Without added parentheses, operator precedence fixes the evaluation order. When you are allowed to parenthesize freely, you can force different split points and therefore different results.
For example, with 2*3+5:
- '
(2*3)+5 = 11' - '
2*(3+5) = 16'
So the same tokens can produce different values depending on how the expression tree is built.
Dynamic Programming Over Subexpressions
A natural state is:
values(i, j) = all possible results from the substring between token i and token j
If you split at an operator k, then every result from the left side can combine with every result from the right side.
For expressions made of single-digit numbers and operators +, -, and *, a memoized recursive solution is straightforward.
This returns every value achievable through legal parenthesization.
Checking Whether a Target Is Reachable
Once you can compute all possible values, checking the target is easy:
This is often the exact form of the interview or algorithm question: return a boolean instead of the full set.
Reconstructing One Valid Parenthesization
If you need not only the answer but also one parenthesized expression that reaches the target, store expressions along with values.
This is more expensive, but it is useful if the problem asks for an actual parenthesization instead of only a yes-or-no answer.
Complexity Discussion
The number of possible parenthesizations grows quickly, so the set of achievable values can also grow quickly. Dynamic programming helps because every substring is solved once and memoized, but the problem is still inherently combinatorial.
That means the DP solution is practical for moderate expression sizes, not arbitrarily large ones.
Common Pitfalls
The biggest mistake is trying to solve the problem with only normal precedence rules. The whole point is that parenthesization creates alternative parse trees.
Another mistake is recomputing the same substring repeatedly without memoization. That turns a manageable dynamic-programming problem into a much slower recursive search.
People also forget that the set of possible values may contain duplicates from different parenthesizations. A set is usually the right data structure when you only care about reachable values.
Finally, define the allowed operator set clearly. Supporting +, -, and * is straightforward, but adding division or unary operators changes the edge cases significantly.
Summary
- Parenthesization changes the expression tree and therefore can change the final value.
- A standard solution uses dynamic programming over subexpressions.
- Memoization avoids recomputing the same substring many times.
- You can compute all reachable values, then test whether the target is present.
- If needed, store one expression per value to reconstruct a valid parenthesization.
Related reading
- Parsing one terabyte of text and efficiently counting the number of occurrences of each word
- Partition a collection into k close-to-equal pieces Scala, but language agnostic
- Partition a set into k groups with minimum number of moves
- Partition an array in order
- partitioning an float array into similar segments clustering
- Partitioning big rectangle to small ones 2D Packing
- Pass std algos predicates by reference in C
- Passing function objects into std algorithms by reference

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.