FastPrepFastPrep
Problem Brief

Minimize Expression with Parentheses

INTERNOA

You are given a string expression that represents a positive arithmetic expression containing exactly one plus sign '+'. The string contains only digits and the plus sign.

Insert exactly one left parenthesis somewhere to the left of the plus sign and exactly one right parenthesis somewhere to the right of the plus sign. After inserting the parentheses, evaluate the resulting expression under the rule that any value immediately outside the parentheses is multiplied with the value inside.

Return the parenthesized expression that produces the minimum possible numeric result.

For example, if expression = "1639+5628", then 16(39+5)628 is a valid candidate because the parts outside the parentheses are multiplied with the parenthesized sum.

1Example 1

Input
expression = "1639+5628"
Output
"16(39+5)628"
Explanation

Placing the parentheses around 39+5 gives the minimum value among all valid choices for this expression.

public String minimizeExpressionWithParentheses(String expression) {
  // write your code here
}
Input

expression

"1639+5628"

Output

"16(39+5)628"

Sign in to submit your solution.