Minimize Result by Adding Parentheses to Expression - Problem

You are given a 0-indexed string expression of the form "<num1>+<num2>" where <num1> and <num2> represent positive integers.

Add a pair of parentheses to expression such that after the addition of parentheses, expression is a valid mathematical expression and evaluates to the smallest possible value. The left parenthesis must be added to the left of '+' and the right parenthesis must be added to the right of '+'.

Return expression after adding a pair of parentheses such that expression evaluates to the smallest possible value. If there are multiple answers that yield the same result, return any of them.

The input has been generated such that the original value of expression, and the value of expression after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer.

Input & Output

Example 1 — Basic Case
$ Input: expression = "247+38"
Output: "2(47+38)"
💡 Note: We can place parentheses as: (247+38)=285, 2(47+38)=2*85=170, 24(7+38)=24*45=1080. The minimum is 170.
Example 2 — Different Numbers
$ Input: expression = "12+34"
Output: "1(2+34)"
💡 Note: Possible placements: (12+34)=46, 1(2+34)=1*36=36. The minimum is 36, so we return "1(2+34)".
Example 3 — Single Digits
$ Input: expression = "2+3"
Output: "(2+3)"
💡 Note: Only one valid placement: (2+3)=5. This is the minimum and only option.

Constraints

  • 1 ≤ expression.length ≤ 10
  • expression consists of digits and exactly one '+'
  • All numbers in expression are positive integers

Visualization

Tap to expand
Minimize Result by Adding Parentheses INPUT Expression String: 2 4 7 + 3 8 num1 = "247" num2 = "38" Placement Options: "(" can go at: 0, 1, 2 ")" can go at: 4, 5 pos: 0 1 2 + 3 4 2 4 7 + 3 8 expression = "247+38" Find min value placement ALGORITHM STEPS 1 Find '+' position Split into num1, num2 2 Enumerate all placements '(' in [0,len1), ')' in [0,len2] 3 Calculate each value left * (a+b) * right 4 Track minimum Update best result Sample Evaluations: (247+38) = 285 (247+3)*8 = 2000 2*(47+38) = 170 2*(47+3)*8 = 800 24*(7+38) = 1080 24*(7+3)*8 = 1920 MIN! FINAL RESULT Optimal Parentheses Position: ( 2 4 7 + 3 8 ) Output: "(247+38)" Evaluation: left prefix: "" --> 1 inner sum: 247+38 = 285 right suffix: "" --> 1 Result: 1 * 285 * 1 = 285 Key Insight: The expression a(b+c)d evaluates to a*b + a*c + a*d*b + a*d*c where parts outside parentheses multiply. With small input sizes (num1, num2 up to 4 digits each), we can enumerate all O(n*m) combinations. Empty prefix/suffix is treated as 1 (multiplicative identity). Here, placing () around entire sum gives minimum. TutorialsPoint - Minimize Result by Adding Parentheses to Expression | Systematic Enumeration
Asked in
Google 15 Microsoft 12
25.0K Views
Medium Frequency
~15 min Avg. Time
800 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen