282-expression-add-operators

Question

Given a string that contains only digits 0-9 and a target value, return all possibilities to add binary operators (not unary) +, -, or *between the digits so they evaluate to the target value.

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"]

Example 4:

Input: num = "00", target = 0
Output: ["0+0", "0-0", "0*0"]

Example 5:

Input: num = "3456237490", target = 9191
Output: []

Thought Process

  1. Divide and Conquer
    1. Similar to 241, we can divide the number at each position and check if there is way to get target using different operation
    2. Time complexity O(?)
    3. Space complexity O(?)
  2. asd

Solution



Additional

results matching ""

    No results matching ""