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


def digits_to_num(digits):
    return int(digits)

def combinations(digits, curr_idx, curr_right, target, cache):
    if curr_idx == len(digits):
        if target == curr_right:
            return ['']
        else:
            return []

    sol = []

    for i in range(curr_idx, len(digits)):
        if digits[curr_idx] != '0' or i == curr_idx:
            next_num = digits_to_num(digits[curr_idx:i + 1])
            # right + X
            for comb in cached_combinations(digits, i + 1, next_num, target - curr_right, cache):
                sol.append(f"+{next_num}{comb}")
            # right - X
            for comb in cached_combinations(digits, i + 1, -next_num, target - curr_right, cache):
                sol.append(f"-{next_num}{comb}")
            # right * X
            for comb in cached_combinations(digits, i + 1, curr_right * next_num, target, cache):
                sol.append(f"*{next_num}{comb}")
    return sol

def cached_combinations(digits, curr_idx, curr_right, target, cache):
    cache_idx = (curr_idx, curr_right, target)

    if cache_idx not in cache:
        cache[cache_idx] = combinations(digits, curr_idx, curr_right, target, cache)
    return cache[cache_idx]

def calc_combinations(digits, target):
    cache = {}
    sol = []
    for i in range(1, len(digits) + 1):
        if digits[0] != '0' or i == 1:
            sol.extend(digits[0:i] + comb for comb in cached_combinations(digits, i, int(digits[0:i]), target, cache))
    return sol


class Solution:
    def addOperators(self, num: str, target: int) -> List[str]:
        return calc_combinations(num, target)
48300