<p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>&#39;+&#39;</code><em>, </em><code>&#39;-&#39;</code><em>, and/or </em><code>&#39;*&#39;</code><em> between the digits of </em><code>num</code><em> so that the resultant expression evaluates to the </em><code>target</code><em> value</em>.</p>

<p>Note that operands in the returned expressions <strong>should not</strong> contain leading zeros.</p>

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

<pre>
<strong>Input:</strong> num = &quot;123&quot;, target = 6
<strong>Output:</strong> [&quot;1*2*3&quot;,&quot;1+2+3&quot;]
<strong>Explanation:</strong> Both &quot;1*2*3&quot; and &quot;1+2+3&quot; evaluate to 6.
</pre>

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

<pre>
<strong>Input:</strong> num = &quot;232&quot;, target = 8
<strong>Output:</strong> [&quot;2*3+2&quot;,&quot;2+3*2&quot;]
<strong>Explanation:</strong> Both &quot;2*3+2&quot; and &quot;2+3*2&quot; evaluate to 8.
</pre>

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

<pre>
<strong>Input:</strong> num = &quot;3456237490&quot;, target = 9191
<strong>Output:</strong> []
<strong>Explanation:</strong> There are no expressions that can be created from &quot;3456237490&quot; to evaluate to 9191.
</pre>

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

<ul>
	<li><code>1 &lt;= num.length &lt;= 10</code></li>
	<li><code>num</code> consists of only digits.</li>
	<li><code>-2<sup>31</sup> &lt;= target &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

class Solution {
public:
    // opt1
    // backtrack
    // time=4^n
    vector<string> results;

    auto eval(const string& expression) {
        auto res = 0ll;
        auto prev = 0ll;
        auto curr = 0ll;
        auto idx = 0ll;
        auto nextNegative = false;
        auto mult = false;
        while(idx < expression.size()) {
            switch(expression[idx]) {
                case '+': 
                    res += curr;
                    break;
                case '-':
                    res += curr;
                    nextNegative = true;
                    break;
                case '*':
                    prev = curr;
                    mult = true;
                    break;
                default: {
                    while(idx < expression.size() && isdigit(expression[idx])) {
                        curr = 10 * curr + (expression[idx] - '0');
                        ++idx;
                    }

                    if(nextNegative)
                        curr = -1 * curr;

                    nextNegative = false;

                    if(mult) {
                        curr = prev * curr;
                        mult = false;
                    }
                    continue;
                }
            }
            curr = 0;
            ++idx;
        }

        if(mult) {
            res = prev * curr;
        } else {
            res += curr;
        }

        return res;
    }

    void backtrack(const string& num, string& expression, int idx, int64_t target, char firstDigit) {
        if(num.size() == idx) {
            // try eval
            if(eval(expression) == target)
                results.push_back(expression);
            
            return;
        }
        
        auto tmp2 = firstDigit;

        for(auto op = 0; op < 4; ++op) {
            auto tmp = expression;

            switch(op) {
                case 0:
                    if(firstDigit == -1)
                        firstDigit = num[idx];
                    else if(firstDigit == '0')
                        continue; // no leading
                    expression += num[idx];
                    break;
                case 1:
                    expression += string("+") + num[idx];
                    firstDigit = num[idx];
                    break;
                case 2:
                    expression += string("-") + num[idx];
                    firstDigit = num[idx];
                    break;
                case 3:
                    expression += string("*") + num[idx];
                    firstDigit = num[idx];
                    break;
            }

            backtrack(num, expression, idx + 1, target, firstDigit);
            expression = tmp;
            firstDigit = tmp2;
        }
    }

    vector<string> addOperators(string num, int64_t target) {
        auto expr = string(1, num[0]);

        backtrack(num, expr, 1, target, num[0]);

        return results;
    }
};
25334