Expression Add Operators

Given a string num that contains only digits and an integer target, return all possibilities to insert the binary operators '+', '-', and/or '*' between the digits of num so that the resultant expression evaluates to the target value.

Operands in the returned expressions should not contain leading zeros.

Note that a number can contain multiple digits.

Example 1
Inputnum = "123", target = 6
Output["1*2*3","1+2+3"]
Both "1*2*3" and "1+2+3" evaluate to 6.
Example 2
Inputnum = "232", target = 8
Output["2*3+2","2+3*2"]
Both "2*3+2" and "2+3*2" evaluate to 8.

Constraints

  • 1 <= num.length <= 10
  • num consists of only digits.
  • -2^31 <= target <= 2^31 - 1

Asked at 7 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate