<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]:
        res_dict = {}
        recursion(num, 0, res_dict)
    
        res = []
        for exp in res_dict[0]:
            if calculate(exp, target):
                res.append(exp)
        
        return res

def recursion(num: str, start_index, res_dict):
    # return possible combination with value

    if not num:
        return 0
    
    if start_index in res_dict:
        return 0

    if len(num[start_index:]) > 1 and num[start_index] == '0':
        res_dict[start_index] = []
    else:
        res_dict[start_index] = [num[start_index:]]
    if len(num) == 1:
        return 0
    
    exp = []
    for i in range(start_index, len(num)-1):
        first_ele = num[start_index:i+1]
        #print(first_ele, fi)
        if len(first_ele) > 1 and first_ele[0] == '0':
            res_dict[start_index].extend(exp)
            return 0
        second_exp = num[i+1:len(num)]
        recursion(num, i+1, res_dict)
        if i+1 in res_dict and res_dict[i+1]:
            for element in res_dict[i+1]:
                for op in ['+', '-', '*']:
                    exp.append(first_ele+op+element)

    res_dict[start_index].extend(exp)
    return 0

def calculate(exp, target):
    stack = []
    cur_res = 0
    i = 0
    exp += '&'
    while i < len(exp):
        if exp[i].isdigit():
            cur_res = cur_res * 10 + int(exp[i])
        else:
            if len(stack) >= 1 and stack[-1] == '*':
                stack.pop()
                cur_res = cur_res * stack[-1]
                stack.pop()

            stack.append(cur_res)
            stack.append(exp[i])
            cur_res = 0
    
        i += 1

    if len(stack) == 1:
        if stack[0] == target:
            return True
        else:
            return False


    res = stack[0]
    for i in range(2, len(stack), 2):
        if stack[i-1] == '+':
            res = res + stack[i]
        else:
            res = res - stack[i]
    return True if target == res else False
        

                








    
    
    

    # 11111111
    # 


     # 1234
     # 1 + 222
     # 1 * 234
     # 1* 2+3
     # 1 * 2 + 3 44

48200