LeetCode 241, Medium. Topics: Math, String, Dynamic Programming, Recursion, Memoization, Bracket Sequences. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 16 parametrized pytest cases, and a playground notebook:
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
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, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026