Description

Given a string num containing only digits and an integer target, return all possibilities to insert the binary operators '+', '-', and/or '*' between the digits so they evaluate to the target value.

Examples

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

Both expressions evaluate to 6.

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

Both expressions evaluate to 8.

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

This example demonstrates handling numbers with zeros and multi-digit operands. "1*0+5" evaluates as (1*0)+5 = 0+5 = 5, and "10-5" treats "10" as a single number, evaluating as 10-5 = 5. Note that expressions like "1+0*5" would equal 1, not 5, due to operator precedence.

Constraints

  • 1 ≤ num.length ≤ 10
  • num consists of only digits.
  • -2³¹ ≤ target ≤ 2³¹ - 1

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!