Skip to content
Decoder

Expression Add Operators

Hard

Problem

Given a string of digits num and an integer target, insert the operators +, - or * between digits (or nothing, which joins digits into a longer number) so that the expression evaluates to target using normal precedence (* before + and -). Return all expressions that work, in any order. Numbers in the expression may not have leading zeros (05 is not allowed, 0 is).

Example 1

Input
num = "123", target = 6
Output
["1+2+3", "1*2*3"]

Example 2

Input
num = "232", target = 8
Output
["2*3+2", "2+3*2"]

Example 3

Input
num = "105", target = 5
Output
["1*0+5", "10-5"]

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.