<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>

# Brute force:
# In between every space, try every operator
# O(n^3)
# Create helper function to evaluate a string
# Numbers can be combined... 
class Solution:
    # Evaluate a string by using a stack
    # If *, then evaluate immediately
    # Second pass to do +/-
    def evaluate(self, num: str):
        # First we need to group the digits together
        expression = []
        currNum = ""
        for i in range(len(num)):
            if num[i] == "+" or num[i] == "-" or num[i] == "*":
                expression.append(currNum)
                expression.append(num[i])
                currNum = ""
            else:
                # We can't eval expressions that have invalid start at 0
                if currNum == "0":
                    return -1
                currNum = currNum + num[i]
        expression.append(currNum)

        # Now iterate through the expression
        multStack = [expression[0]]
        mult = False
        for i in range(1, len(expression)):
            if expression[i] == "*":
                mult = True
            elif expression[i] == "-" or expression[i] == "+":
                multStack.append(expression[i])
            else:
                if mult:
                    multStack[-1] = str(int(multStack[-1]) * int(expression[i]))
                    mult = False
                else:
                    multStack.append(expression[i])
        
        # Mult stack is now the expression without multiplication:
        add = True
        result = int(multStack[0])
        for i in range(1, len(multStack)):
            if multStack[i] == "+":
                add = True
            elif multStack[i] == "-":
                add = False
            else:
                if add:
                    result += int(multStack[i])
                else:
                    result -= int(multStack[i])
        return result

    def createExpressions(self, num, idx, currExpression, expressions):
        if idx == len(num):
            expressions.append(currExpression)
            return
        
        # Create every expression possible
        plus = currExpression + "+" + num[idx]
        sub = currExpression + "-" + num[idx]
        mult = currExpression + "*" + num[idx]
        # There is also a case where we don't add a symbol
        none = currExpression + num[idx]
        self.createExpressions(num, idx+1, plus, expressions)
        self.createExpressions(num, idx+1, sub, expressions)
        self.createExpressions(num, idx+1, mult, expressions)
        self.createExpressions(num, idx+1, none, expressions)

    def addOperators(self, num: str, target: int) -> List[str]:
        # Create every possible expression
        expressions = []
        self.createExpressions(num, 1, num[0], expressions)
        # print(expressions)

        result = []
        for expression in expressions:
            if self.evaluate(expression) == target:
                result.append(expression)

        return result
40800