MediumStringEnumerationUpdated Sep 2026

2232. Minimize Result by Adding Parentheses to Expression

Asked at Capital One

Difficulty

Medium

Time

O(L^2)

Space

O(L)

Pattern

String

Companies

1

Problem

You are given a string of the form "<num1>+<num2>", where both parts are positive integers written in decimal. For example, "247+38".

You must insert one pair of parentheses. The opening ( must go somewhere to the left of the +, and the closing ) must go somewhere to the right of the +. Digits that end up outside the parentheses are multiplied with the parenthesized sum, because in math a number written right next to a bracket means multiplication. So "2(47+38)" means 2 * (47 + 38).

Among all valid placements, return the expression string that evaluates to the smallest value. If several placements give the same smallest value, any one of them is accepted. The expression is very short (at most 10 characters), so you can simply try every placement.

One bracket pair, two cut points

expression(47+38)247+38012345Digits 2 and 8 stay outside, so they multiply the suminside the brackets.value = 2 * (47 + 38) = 2 * 85 = 170
The `(` cuts the left number and the `)` cuts the right number, so the digits outside become multipliers.

Constraints

  • ●3 <= expression.length <= 10
  • ●expression consists only of digits '1' to '9' and exactly one '+'
  • ●expression starts and ends with a digit
  • ●The original value and every value reachable by a valid placement fit in a signed 32-bit integer

Examples

Example 1
Inputexpression = "247+38"
Output"2(47+38)"
WhyHere the `(` goes before `47` and the `)` after `38`. The value is 2 * (47 + 38) = 2 * 85 = 170, which is the smallest value any placement can produce.
Example 2
Inputexpression = "12+34"
Output"1(2+3)4"
WhyThe `(` goes before `2` and the `)` after `3`. The value is 1 * (2 + 3) * 4 = 1 * 5 * 4 = 20. Putting the brackets anywhere else gives a larger number.
Example 3
Inputexpression = "999+999"
Output"(999+999)"
WhyThe `(` goes at the very start and the `)` at the very end, so nothing is multiplied in. The value is just 999 + 999 = 1998, and any other placement multiplies by a leftover digit and becomes bigger.

Asked At

1 companies
CompanyDifficulty
Capital OneMediumView all Capital One questions →

How to Think About It

1

Split at + into left and right. The open parenthesis goes before some digit of left, and the close parenthesis goes after some digit of right.

2

For an open position i and close position j: a = left[:i] (multiplier, 1 if empty), b = left[i:], c = right[:j+1], d = right[j+1:] (multiplier, 1 if empty). The value is a * (b + c) * d.

3

Both sides have at most 9 digits, so there are at most 9 * 9 = 81 placements — brute force is the intended solution.

4

Track the minimum value and build the output string for it.

5

Walkthrough for "247+38": placing ( before 47 and ) after 38 gives 2 * (47 + 38) = 170, the minimum.

Optimal Approach

Step 1: left, right = expression.split("+").
Step 2: For i in 0..len(left)-1 and j in 0..len(right)-1:
a = int(left[:i]) or 1, b = int(left[i:]), c = int(right[:j+1]), d = int(right[j+1:]) or 1.
val = a * (b + c) * d; keep the best and its string left[:i] + "(" + left[i:] + "+" + right[:j+1] + ")" + right[j+1:].
Step 3: Return the best string.

Time: O(L²) placements with O(L) parsing each. Space: O(L).

Try every cut point

left = 247247012ii = 1 means a = "2" (multiplier) and b = "47" (insidethe brackets).right = 383801jj = 1 means c = "38" (inside) and d = "" so themultiplier is 1.a = left[:i] or 1b = left[i:]c = right[:j+1]d = right[j+1:] or 1value = a * (b + c) * d
Loop over the open position i and the close position j, compute the value, and keep the smallest.
ComplexityTime O(L^2)Space O(L)There are at most 9 * 9 placements, and each one parses a few substrings of length O(L), so the work is quadratic in the expression length.

What Trips People Up in Real Interviews

1

Treating an empty prefix or suffix as 0 instead of 1. An empty multiplier means "no multiplication".

2

Allowing the parentheses to exclude every digit on one side — each side of the + must keep at least one digit inside.

3

Looking for a clever greedy. With at most 81 options, brute force is the right call — say so.

4

Integer overflow in C++/Java: products of up to 9-digit numbers need 64-bit arithmetic.

Solution Code

O(L^2) time
def minimizeResult(expression):
    left, right = expression.split('+')
    best_val = None
    best = ''
    for i in range(len(left)):
        for j in range(len(right)):
            a = int(left[:i]) if i > 0 else 1
            b = int(left[i:])
            c = int(right[:j + 1])
            d = int(right[j + 1:]) if j + 1 < len(right) else 1
            val = a * (b + c) * d
            if best_val is None or val < best_val:
                best_val = val
                best = left[:i] + '(' + left[i:] + '+' + right[:j + 1] + ')' + right[j + 1:]
    return best

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Minimize Result by Adding Parentheses to Expression problem?

Minimize Result by Adding Parentheses to Expression gives you a string like `"247+38"` and asks where to place one pair of parentheses around the `+` so the value is as small as possible, where digits outside the parentheses multiply the parenthesized sum — `"2(47+38)"` means `2 * (47 + 38) = 170`. The expression is short, so enumerate every placement.

How do you solve Minimize Result by Adding Parentheses to Expression?

The optimal approach runs in O(L^2) time and O(L) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Minimize Result by Adding Parentheses to Expression?

Minimize Result by Adding Parentheses to Expression is asked at Capital One. It is a medium difficulty problem.

What are common mistakes on Minimize Result by Adding Parentheses to Expression?
  • Treating an empty prefix or suffix as 0 instead of 1. An empty multiplier means "no multiplication".
  • Allowing the parentheses to exclude every digit on one side — each side of the `+` must keep at least one digit inside.
  • Looking for a clever greedy. With at most 81 options, brute force is the right call — say so.
  • Integer overflow in C++/Java: products of up to 9-digit numbers need 64-bit arithmetic.