> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Expression Add Operators Python Solution

> Tested Python solution for LeetCode 282 with 24 pytest cases. Generate a practice environment with lcpy.

LeetCode 282, [Hard](/catalog/hard). Topics: [Math](/catalog/topics/math), [String](/catalog/topics/string), [Backtracking](/catalog/topics/backtracking). [View on LeetCode](https://leetcode.com/problems/expression-add-operators/description/).

Generate this problem as a practice environment: tested reference solution, 24 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 282   # by problem number
lcpy gen -s expression_add_operators   # by problem name
```

## 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

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

**Explanation:** Both `"1*2*3"` and `"1+2+3"` evaluate to 6.

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

**Explanation:** Both `"2*3+2"` and `"2+3*2"` evaluate to 8.

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

**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](https://github.com/wislertt/leetcode-py/blob/main/leetcode/expression_add_operators/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/expression_add_operators/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(4^n / sqrt(n)) operator placements, each O(n) to extend
    # Space: O(n) recursion depth plus the output
    def add_operators(self, num: str, target: int) -> list[str]:
        results: list[str] = []
        n = len(num)

        def backtrack(index: int, expr: list[str], value: int, prev_operand: int) -> None:
            if index == n:
                if value == target:
                    results.append("".join(expr))
                return

            for end in range(index + 1, n + 1):
                operand_str = num[index:end]
                # Reject operands with leading zeros ("05", "00"), allow plain "0"
                if len(operand_str) > 1 and operand_str[0] == "0":
                    break
                operand = int(operand_str)

                if index == 0:
                    expr.append(operand_str)
                    backtrack(end, expr, operand, operand)
                    expr.pop()
                    continue

                expr.append("+")
                expr.append(operand_str)
                backtrack(end, expr, value + operand, operand)
                expr.pop()
                expr.pop()

                expr.append("-")
                expr.append(operand_str)
                backtrack(end, expr, value - operand, -operand)
                expr.pop()
                expr.pop()

                multiplied = prev_operand * operand
                expr.append("*")
                expr.append(operand_str)
                backtrack(end, expr, value - prev_operand + multiplied, multiplied)
                expr.pop()
                expr.pop()

        backtrack(0, [], 0, 0)
        return results
```

## Complexity

| Time | Space |
| - | - |
| O(4^n / sqrt(n)) operator placements, each O(n) to extend | O(n) recursion depth plus the output |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.