<p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p>

<p>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 <code>10<sup>4</sup></code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> expression = &quot;2-1-1&quot;
<strong>Output:</strong> [0,2]
<strong>Explanation:</strong>
((2-1)-1) = 0 
(2-(1-1)) = 2
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> expression = &quot;2*3-4*5&quot;
<strong>Output:</strong> [-34,-14,-10,-10,10]
<strong>Explanation:</strong>
(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
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= expression.length &lt;= 20</code></li>
	<li><code>expression</code> consists of digits and the operator <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;*&#39;</code>.</li>
	<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
</ul>

class Solution {

    map<pair<int, int>, vector<int>> cache;  // (start, end) -> possible values

public:

    vector<int> diffWaysToComputePart(string expression, int start, int end) {
        
        if (this->cache.find({start, end}) != this->cache.end())
            return this->cache[{start, end}];  // calculated once before
        if (end - start + 1 <= 2) {  // in the range [0, 99]
            this->cache[{start, end}] = {stoi(expression.substr(start, end - start + 1))};
            return {stoi(expression.substr(start, end - start + 1))};
        }

        vector<int> ans;  // start recursion
        for (int i = start + 1; i < end; ++i) {
            if (expression[i] == '+' || expression[i] == '-' || expression[i] == '*') {
                vector<int> left = diffWaysToComputePart(expression, start, i - 1);
                vector<int> right = diffWaysToComputePart(expression, i + 1, end);
                for (int l: left)
                    for (int r: right)
                        if (expression[i] == '+')
                            ans.push_back(l + r);
                        else if (expression[i] == '-')
                            ans.push_back(l - r);
                        else  // '*'
                            ans.push_back(l * r);
            }
        }

        this->cache[{start, end}] = ans;
        return ans;
    }

    vector<int> diffWaysToCompute(string expression) {
        return diffWaysToComputePart(expression, 0, expression.size() - 1);
    }
};
9800