<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 {
public:
    // This is the recursive helper function
    void aORec(string num, int target, vector<string>& ans, int ind, string curr, long prev, long res) {
        // Base case: if we've reached the end of the string
        if (ind == num.size()) {
            // Check if the current result matches the target
            if (target == res) {
                ans.push_back(curr);  // Add the current expression to the result
            }
            return;
        }

        string st = "";         // To build the current substring (number)
        long currRes = 0;       // To store the integer value of the current substring

        // Iterate over the remaining part of the string to explore different number splits
        for (int i = ind; i < num.size(); i++) {
            // Leading zero check: If the number starts with '0', we only allow '0' itself (no "05")
            if (i > ind && num[ind] == '0') break;  // Use break instead of return to allow further backtracking

            // Build the current number string (e.g., "1", "10", "105", etc.)
            st += num[i];
            currRes = currRes * 10 + (num[i] - '0');  // Convert string to integer

            // If it's the first number, we can't add operators in front of it, just recurse as it is
            if (ind == 0) {
                // Directly pass the first number to the next recursive call
                aORec(num, target, ans, i + 1, st, currRes, currRes);
            } else {
                // Recursion for addition: add the current number to the result
                aORec(num, target, ans, i + 1, curr + '+' + st, currRes, res + currRes);

                // Recursion for subtraction: subtract the current number from the result
                aORec(num, target, ans, i + 1, curr + '-' + st, -currRes, res - currRes);

                // Recursion for multiplication: this is where we fix precedence
                // Adjust result by removing the last added value (prev) and then adding prev * currRes
                aORec(num, target, ans, i + 1, curr + '*' + st, prev * currRes, res - prev + prev * currRes);
            }
        }
    }

    // The main function
    vector<string> addOperators(string num, int target) {
        vector<string> ans;   // To store valid expressions
        string curr;          // Current expression
        aORec(num, target, ans, 0, curr, 0, 0);  // Start recursion with initial values
        return ans;           // Return the list of valid expressions
    }
};

38903