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

        if len(num) == 1:
            return int(num) == target
        
        lst = [[num[0], int(num[0])]]
        dirs = ['+', '-', '*', '']
        pos = 1
        while pos < len(num):
            new_lst = []
            for s, val in lst:
                for d in dirs:
                    if self.leading0(s+d+num[pos]):
                        continue
                    new_s = s+d+num[pos]
                    if d == '+': new_val = val + int(num[pos])
                    elif d == '-': new_val = val - int(num[pos])
                    else: new_val = eval(new_s)
                    new_lst.append([new_s, new_val])
            lst = new_lst
            pos += 1
        res = []
        for s, val in lst:
            if val == target and not self.leading0(s):
                res.append(s)
        return res

    def leading0(self, s):
        for i in range(len(s)):
            if s[i] == '0' and (i == 0 or s[i-1] in ['+', '-', '*']):
                if i+1 < len(s) and s[i+1].isdigit():
                    return True
        return False

    """
    def addOperators(self, num: str, target: int) -> List[str]:
        # beating 32%.. time 4^n.. space 4^n

        if len(num) == 1:
            return int(num) == target
        
        expr2val = {}
        for i in range(len(num)):
            expr2val[num[i]] = int(num[i])
        lst = [(num[0], int(num[0]), None, num[0])] # [curr_expr, current_expr_result, prev_operator, prev_expr]
        # prev_operator means '+': 1, '-': -1
        pos = 1
        while pos < len(num):
            new_lst = []
            for s, val, prev_optor, prev_expr in lst:
                # attach num[pos]
                if not ((s[-1]=='0' and len(s)>1 and s[-2] in '+-*') or \
                         s[-1]=='0' and len(s)==1):
                    new_expr = prev_expr + num[pos]
                    if new_expr not in expr2val:
                        expr2val[new_expr] = eval(new_expr)
                    if prev_optor is None:
                        new_val = expr2val[new_expr]
                    else:
                        new_val = val - prev_optor * expr2val[prev_expr] + prev_optor * expr2val[new_expr]                        
                    new_lst.append((s+num[pos], new_val, prev_optor, new_expr))
                # add num[pos]
                new_lst.append((s+'+'+num[pos], val+int(num[pos]), 1, num[pos]))
                # minus num[pos]
                new_lst.append((s+'-'+num[pos], val-int(num[pos]), -1, num[pos]))
                # times num[pos]
                new_expr = prev_expr + '*' + num[pos]
                if new_expr not in expr2val:
                    expr2val[new_expr] = eval(new_expr)
                if prev_optor is None:
                    new_lst.append((s+'*'+num[pos], val*int(num[pos]), prev_optor, new_expr))
                else:
                    new_val = val - prev_optor * expr2val[prev_expr] + prev_optor * expr2val[new_expr]
                    new_lst.append((s+'*'+num[pos], new_val, prev_optor, new_expr))
            lst = new_lst
            pos += 1
        res = []
        for s, val, _, _ in lst:
            if val == target:
                res.append(s)
        return res
    """

"""working, beats 10%
class Solution:
    def addOperators(self, num: str, target: int) -> List[str]:

        if len(num) == 1:
            return int(num) == target
        
        lst = [num[0]]
        dirs = ['+', '-', '*', '']
        pos = 1
        while pos < len(num):
            new_lst = []
            for s in lst:
                for d in dirs:
                    if d == '' and self.leading0(s+d+num[pos]):
                        continue
                    new_lst.append(s+d+num[pos])
            lst = new_lst
            pos += 1
        res = []
        for s in lst:
            if eval(s) == target and not self.leading0(s):
                res.append(s)
        return res

    def leading0(self, s):
        for i in range(len(s)):
            if s[i] == '0' and (i == 0 or s[i-1] in ['+', '-', '*']):
                if i+1 < len(s) and s[i+1].isdigit():
                    return True
        return False
"""

"""
    def calcExpr(self, s):

        dirs = ['+', '-', '*', ''] 
        # calc * 
        i = 0
        lst = []
        while i < len(s):
            num = ''
            while i < len(s) and s[i] not in dirs:
                num += s[i]
                i += 1
            if num != '':
                lst.append(num)
            if i < len(s):
                if s[i] in '+-':
                    lst.append(s[i])
                    i += 1
                else:
                    op = s[i]
                    i += 1
                    num2 = ''
                    while i < len(s) and s[i] not in dirs:
                        num2 += s[i]
                        i += 1
                    num1 = lst.pop()
                    if leading0(num1) or leading0(num2):
                        return None
                    num = int(num1) * int(num2) 
                    lst.append(num)
        # calc + -
        while lst:
            if len(lst) == 1:
                break
            num2 = lst.pop()
            op = lst.pop()
            num1 = lst.pop()
            if leading0(num1) or leading0(num2):
                return None
            num = int(num1) + int(num2) if op == '+' else int(num1) - int(num2)
            lst.append(num)
        return int(lst[0]) if not leading0(lst[0]) else None
"""




        
        

78200