Expression Add Operators
Given a string num of digits and an integer target, insert the binary operators +, -, and * (and choose multi-digit operands) between the digits so the resulting arithmetic expression evaluates to target. Return all such expressions. Operands may not have leading zeros.
Open official problem prompt ↗Produce every arithmetic string formed by grouping the digits into operands and inserting +, -, or * that evaluates to the target.
Like filling the gaps between beads on a string with plus, minus, or times signs (or gluing beads into a bigger number), then checking if the whole thing computes to the target; multiplication is tricky because it reaches back and rewrites the previous addition.
- Input
- num = "123", target = 6
- Output
- ["1*2*3","1+2+3"]
- Why
- 1*2*3 = 6 and 1+2+3 = 6 are the only ways to insert operators between 1, 2, 3 to reach 6.
1 <= num.length <= 10num consists of only digits-2^31 <= target <= 2^31 - 1Operands cannot contain leading zeros