<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:
    def addOperators(self, num: str, target: int) -> List[str]:
        
        
        # MAS --> order of operations
        
        def eval_option(x):
            
            i = 0
            total = 0
            
            parts = []
            while True:
                buffer = []
                j = 0
                while i+j < len(x) and x[i+j].isalnum():
                    buffer.append(x[i+j])
                    j += 1
                new_num = int("".join(buffer))
                parts.append(new_num)
                if i+j+1 >= len(x):
                    break
                parts.append(x[i+j])
                i += j + 1
                buffer = []
                
            i = 0
            while i < len(parts):
                part = parts[i]
                if part == "*":
                    r = parts.pop(i+1)
                    parts.pop(i)
                    l = parts.pop(i-1)
                    parts.insert(i-1, l*r)
                    i-=1
                i+=1

            total = 0
            last_op = None
            for part in parts:
                if type(part) == str:
                    last_op = part
                    continue
                if last_op is None:
                    total = part
                if last_op == "+":
                    total += part 
                elif last_op == "-":
                    total -= part
                  
            return total
            
        
        def is_valid(option):
            "check for leading zeros"
            for i, c in enumerate(option):
                if c == "0" and (i == 0 or not option[i-1].isalnum()) and len(option) > i+1 and option[i+1].isalnum():
                    return False
            return True
            
            
        options = []
        
        def create_option(option, idx):
            if idx == len(option):
                options.append(option)
            else:
                for op in ["*","+","-",""]:
                    new_op = option[:idx] + op + option[idx:]
                    new_idx = idx + 1 if op == "" else idx + 2
                    create_option(new_op, new_idx)
                    
        create_option(num, 1)
    
        correct = []
    
        for option in options:
            if not is_valid(option):
                continue
            res = eval_option(option)
            if res == target:
                correct.append(option)
        
        return correct
109700