Skip to main content
LeetCode 282, Hard. Topics: Math, String, Backtracking. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 24 parametrized pytest cases, and a playground notebook:

Problem

Given a string num that contains only digits and an integer target, return all possibilities to insert the binary operators '+', '-', and/or '*' between the digits of num so that the resultant expression evaluates to the target value. Note that operands in the returned expressions should not contain leading zeros. Note that a number can contain multiple digits.

Examples

Explanation: Both "1*2*3" and "1+2+3" evaluate to 6.
Explanation: Both "2*3+2" and "2+3*2" evaluate to 8.
Explanation: There are no expressions that can be created from "3456237490" to evaluate to 9191.

Constraints

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

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026