<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 {
public:
    // function calculates the result for each combination of left and right subarray
    void calVal(vector<int>& target, const vector<int>& ls, const vector<int>& rs, char op) {
        // passing in all the arguments as references to avoid return type
        for (const auto& l : ls) {
            for (const auto& r : rs) {
                switch (op) {
                    case '+': target.push_back(l + r); break;
                    case '-': target.push_back(l - r); break;
                    case '*': target.push_back(l * r); break;
                }
            }
        }
    }

public:
    vector<int> diffWaysToCompute(string expression) {
        vector<int> nums;           
        vector<char> ops;   

        int curr = 0;
        // separate the numbers and operators
        for (const auto& ch : expression) {
            if (isdigit(ch)) {
                curr = curr * 10 + (ch - '0'); 
            } 
            else {
                nums.push_back(curr);          
                curr = 0;                       
                ops.push_back(ch);           
            }
        }
        // add last number
        nums.push_back(curr); 

        int n = nums.size();

        // 3d table to store the results
        vector<vector<vector<int>>> out(n, vector<vector<int>>(n));
        // put items into the table
        for (int len = 0; len < n; ++len) {    
            for (int i = 0; i < n - len; ++i) {
                int j = i + len;              
                if (i == j) {
                    // single number
                    out[i][j].push_back(nums[i]); 
                } 
                else {
                    for (int k = i; k < j; ++k) {
                        // merge results from left and right
                        calVal(out[i][j], out[i][k], out[k + 1][j], ops[k]);
                    }
                }
            }
        }

        return out[0][n - 1];
    }
};
8100