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<sup>4</sup>
.
For example:
If the input is expression = "2-1-1"
The expected output is [0,2]
Because:
((2-1)-1) = 0
(2-(1-1)) = 2
If the input is expression = "2*3-4*5"
The expected output is [-34,-14,-10,-10,10]
Because:
(2*(3-(45))) = -34
((23)-(45)) = -14
((2(3-4))5) = -10
(2((3-4)5)) = -10
(((23)-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]
.'-'
or '+'
denoting the sign.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<sup>4</sup>
.
Example 1:
Input: expression = "2-1-1"
Output: [0,2]
Explanation:
((2-1)-1) = 0
(2-(1-1)) = 2
Example 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
This problem can be solved using recursion and dynamic programming (memoization) to avoid redundant calculations. The basic idea is to split the expression at each operator, recursively evaluate the left and right sub-expressions, and then combine the results based on the operator.
def diffWaysToCompute_naive(expression):
if expression.isdigit():
return [int(expression)]
results = []
for i, char in enumerate(expression):
if char in ['+', '-', '*']:
left_results = diffWaysToCompute_naive(expression[:i])
right_results = diffWaysToCompute_naive(expression[i+1:])
for left in left_results:
for right in right_results:
if char == '+':
results.append(left + right)
elif char == '-':
results.append(left - right)
else:
results.append(left * right)
return results
def diffWaysToCompute(expression):
memo = {}
def compute(expr):
if expr in memo:
return memo[expr]
if expr.isdigit():
return [int(expr)]
results = []
for i, char in enumerate(expr):
if char in ['+', '-', '*']:
left_results = compute(expr[:i])
right_results = compute(expr[i+1:])
for left in left_results:
for right in right_results:
if char == '+':
results.append(left + right)
elif char == '-':
results.append(left - right)
else:
results.append(left * right)
memo[expr] = results
return results
return compute(expression)
expression1 = "2-1-1"
print(diffWaysToCompute(expression1)) # Output: [0, 2]
expression2 = "2*3-4*5"
print(diffWaysToCompute(expression2)) # Output: [-34, -14, -10, -10, 10]
n
is the length of the expression, we can say the runtime is O(n * number of sub-expressions), where the number of sub-expressions is still related to Catalan numbers but each sub-expression is computed only once.