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

# Different Ways to Add Parentheses

> Tested Python solution for LeetCode 241 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 241, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math), [String](/catalog/topics/string), [Dynamic Programming](/catalog/topics/dynamic-programming), [Recursion](/catalog/topics/recursion), [Memoization](/catalog/topics/memoization), Bracket Sequences. [View on LeetCode](https://leetcode.com/problems/different-ways-to-add-parentheses/description/).

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

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

## Problem

Given a string `expression` of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. You may return the answer in any order.

The test cases are generated such that the output values fit in a 32-bit integer and the number of different results does not exceed 10^4.

### Examples

```
Input: expression = "2-1-1"
Output: [0,2]
Explanation:
((2-1)-1) = 0
(2-(1-1)) = 2
```

```
Input: expression = "2*3-4*5"
Output: [-34,-14,-10,-10,10]
Explanation:
(2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10
```

### Constraints

* 1 \<= expression.length \<= 20
* expression consists of digits and the operator '+', '-', and '\*'.
* All the integer values in the input expression are in the range \[0, 99].
* The integer values in the input expression do not have a leading '-' or '+' denoting the sign.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/different_ways_to_add_parentheses/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/different_ways_to_add_parentheses/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * 2^n)
    # Space: O(n * 2^n)
    def diff_ways_to_compute(self, expression: str) -> list[int]:
        results: list[int] = []
        for i, ch in enumerate(expression):
            if ch in "+-*":
                left = self.diff_ways_to_compute(expression[:i])
                right = self.diff_ways_to_compute(expression[i + 1 :])
                for left_val in left:
                    for right_val in right:
                        if ch == "+":
                            results.append(left_val + right_val)
                        elif ch == "-":
                            results.append(left_val - right_val)
                        else:
                            results.append(left_val * right_val)
        if not results:
            results.append(int(expression))
        return results
```

## Complexity

| Time | Space |
| - | - |
| O(n \* 2^n) | O(n \* 2^n) |

## Tags

[NeetCode All](/catalog/neetcode).


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