FastPrepMinimum Value After Parenthesizing a Sum

Minimum Value After Parenthesizing a Sum

Pinterest logoPinterest● MediumINTERNOA

Problem statement

The string expr contains two positive integers separated by +. Neither integer contains the digit0. Insert exactly one matching pair of parentheses around the plus sign, with at least one digit on each side of the plus sign inside the parentheses.

A number immediately outside the parentheses multiplies the parenthesized sum. A missing outside number contributes a factor of1. Return the minimum numeric value among all legal placements.

Function

minimumExpressionValue(expr: String) → int

Examples

Example 1

expr = "112+422"return = 108

The placement1(12+42)2 has value1 * 54 * 2 = 108. Checking every legal position for the two parentheses gives no smaller value.

Constraints

  • 3 <= expr.length <= 13.
  • expr consists of exactly two integers separated by one + sign. Each integer has between1 and6 digits, all chosen from1 through9.

More Pinterest problems

See Pinterest hiring insights
Saved
public int minimumExpressionValue(String expr) {
    // Write your solution here.
}
Checking account…