Ctrl K

Signs Between the Digits

Hard
You are given a string digits made of digits and an integer goal. Between any two neighboring digits you may put one of the signs +, -, *, or nothing at all, in which case the digits join into a longer number. The order of the digits stays fixed. Return every resulting expression whose value (with the usual precedence: multiplication before addition and subtraction) equals goal. Return the expressions as strings without spaces, in any order. Numbers in an expression must not have leading zeros: 0 is fine, 05 or 00 is not.
Example 1
digits="2024", goal=8 → ['2+0+2+4', '2-0+2+4', '2*0+2*4']

Exactly three expressions give 8: 2+0+2+4, 2-0+2+4 and 2*0+2*4.

Example 2
digits="105", goal=5 → ['1*0+5', '10-5']

1*0+5 = 5 and 10-5 = 5. 1*05 is not allowed because 05 has a leading zero.

Example 3
digits="99", goal=1 → []

Two nines only give 99, 18, 0 and 81; none of them is 1.

Constraints
  • 1 ≤ len(digits) ≤ 10
  • digits consists of digits only
  • -2³¹ ≤ goal ≤ 2³¹ - 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLetter Sum Cipher HardZero-Free Number With a Multiple Digit Product HardEvery Way to Space the Words HardWords Hidden in a Letter Grid HardPlus-Minus Calculator With Brackets Hard
loading Python…
Input
digits="2024", goal=8
Expected
['2+0+2+4', '2-0+2+4', '2*0+2*4']

Run checks the first 3 tests in your browser. Submit checks all 69 tests on the server, hidden ones included.