<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]:
        """
        :type num: str
        :type target: int
        :rtype: List[str]
        """
        to_ret = []
        combos = []

        self.get_substrings(num, 1, "", combos)
        for equation in combos:
            equation_array = self.equation_to_array(equation)
            if not self.has_leading_0s(equation_array):
                result = self.calculate_result(equation_array)
                if result == target:
                    to_ret.append(equation)
        return to_ret

    def has_leading_0s(self, equation_array):
        for x in equation_array:
            if not(x == "+" or x == "-" or x == "*"):
                if len(x) > 1 and x[0] == "0":
                    return True
        return False

    def calculate_result(self, equation_array):
        idx = 0
        
        while idx < len(equation_array):
            if equation_array[idx] == "*":
                product = int(equation_array[idx - 1]) * int(equation_array[idx + 1])
                equation_array[idx - 1] = product
                equation_array.pop(idx)
                equation_array.pop(idx)
            else:
                idx += 1
        
        result = 0
        positive = True

        for x in equation_array:
            if x == "+":
                positive = True
            elif x == "-":
                positive = False
            else:
                if positive:
                    result += int(x)
                else:
                    result -= int(x)
        return result

    def equation_to_array(self, equation):
        equation_array = []
        tracker = ""
        for letter in equation: 
            if letter == "*" or letter == "+" or letter == "-":
                equation_array.append(tracker)
                equation_array.append(letter)
                tracker = ""
            else:
                tracker += letter
        equation_array.append(tracker)
        return equation_array
    
    def get_substrings(self, num, idx, curr_string, accum):
        curr_num = num[idx-1]
        if idx == len(num):
            accum.append(f"{curr_string}{curr_num}")
            return
        
        self.get_substrings(num, idx + 1, f"{curr_string + curr_num}+", accum)
        self.get_substrings(num, idx + 1, f"{curr_string + curr_num}-", accum)
        self.get_substrings(num, idx + 1, f"{curr_string + curr_num}*", accum)
        self.get_substrings(num, idx + 1, f"{curr_string + curr_num}", accum)

    #     num_list = list(map(lambda x: int(x), list(num)))

    #     if len(num_list) == 1:
    #         return num == str(target)

    #     # returns a list of operators
    #     all_possible = self.helper(num_list, 1, [])
    #     to_ret = []

    #     for combo in all_possible:
    #         assert(len(combo) == (len(num_list) - 1))
    #         if self.combo_adds_up_to_target(combo, num_list, target):
    #             to_ret.append(self.make_statement(combo, num_list))
    #     return to_ret

    # def make_statement(self, combo, num_list):
    #     statement = str(num_list[0])
    #     for idx, op in enumerate(combo):
    #         statement += op + str(num_list[idx+1])
    #     return statement

    # def combo_adds_up_to_target(self, combo, num_list, target):
    #     idx = 0
    #     result = num_list[0]
    #     while idx < len(combo):
    #         op = combo[idx] # -
    #         next_op = None # *
    #         next_num = num_list[idx + 1] # 3

    #         if idx + 1 < len(combo):
    #             next_op = combo[idx + 1]

    #         next_idx = idx + 1 # 1
    #         mult_acc_num = None


    #         if next_op == "*":
    #             next_idx, mult_acc_num = self.get_mult_acc(combo, num_list, num_list[idx + 1], next_idx)

    #         if op == "+":
    #             if mult_acc_num == None:
    #                 result += next_num
    #             else:
    #                 result += mult_acc_num
    #         elif op == "-":
    #             if mult_acc_num == None:
    #                 result -= next_num
    #             else:
    #                 result -= mult_acc_num
    #         else: 
    #             if mult_acc_num == None:
    #                 result *= next_num
    #             else: 
    #                 result *= mult_acc_num
    #         idx = next_idx
        
    #     return result == target

    
    # def get_mult_acc(self, combo, num_list, starting, idx):
    #     mult_acc = starting # 3
    #     while idx < len(combo) and combo[idx] == "*":
    #         mult_acc *= num_list[idx + 1]
    #         idx += 1
    
    #     return (idx, mult_acc)

                
    
    # def helper(self, num_list, num_idx, acc_of_operators):
    #     if num_idx >= len(num_list):
    #         return acc_of_operators
        
    #     list_with_plus = self.helper(num_list, num_idx + 1, self.addToEndForAll(acc_of_operators, "+"))
    #     list_with_minus = self.helper(num_list, num_idx + 1, self.addToEndForAll(acc_of_operators, "-"))
    #     list_with_times = self.helper(num_list, num_idx + 1, self.addToEndForAll(acc_of_operators, "*"))

    #     return list_with_plus + list_with_minus + list_with_times

    # def addToEndForAll(self, list_of_list, extra):
    #     if len(list_of_list) == 0:
    #         return [[extra]]
    #     to_ret = []
    #     for l in list_of_list:
    #         new_list = list(l)
    #         new_list.append(extra)
    #         to_ret.append(new_list)
    #     return to_ret

41000