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

/*
    Brute force -> try every single operator at every single between
    position in the array, and for each possible solution, evaluate

    Lots of annoyance -> doable.

    Order of operations land:
        A*B*C +D+E+ F*H*I*J

        A+B+ => waitingAdd = (A+B)

        waitingAdd = a, waitingTimes = t 
        (A+B+C*D+) (D*A*B*C)
        
        (A...B+) (D*A*B)

    @note maybe we take a target to our function that we update.
    @note: we can start our pos at 1
*/
enum class Operation
{
    Times, Plus, Minus, Join
};

template<Operation operation>
struct Join;

struct State
{
    long long toAdd;
    long long toTimes;
    long long toJoin;
};

class Solution {
    vector<int> m_digits;
    long long target;
public:
    using ret_t = vector<string>;
    ret_t getPossibilities(
        size_t pos,
        State priorState,
        Operation priorOp)
    {
        auto toAdd = priorState.toAdd, toTimes = priorState.toTimes, toJoin = priorState.toJoin;
        if(pos == m_digits.size() - 1)
        {
            size_t currVal = [&]()
            {
                switch(priorOp)
                {
                case Operation::Join:
                    return ((10*toJoin + m_digits[pos])* toTimes) + toAdd;
                case Operation::Times:
                    return (m_digits[pos] * toTimes) + toAdd;
                case Operation::Minus:
                    return (toAdd - m_digits[pos]);
                case Operation::Plus:
                    return (toAdd + m_digits[pos]);
                }
                throw std::runtime_error{"bad case"};
            }();
            if(currVal == target)
            {
                return {std::to_string(m_digits[pos])};
            }
            return {};
        }

        
        ret_t res;
        // PLUS
        // reconsile all states
        for(auto& elt : getPossibilities(
            pos + 1, 
            {
                .toAdd = ((m_digits[pos] + 10*toJoin) * toTimes) + toAdd,
                .toTimes = 1,
                .toJoin = 0
            },
            Operation::Plus))
        {
            res.push_back(std::to_string(m_digits[pos]) + "+" + elt);
        }
        // MINUS
        for(auto& elt : getPossibilities(
            pos + 1, 
            {
                .toAdd = ((m_digits[pos] + 10*toJoin) * toTimes) + toAdd,
                .toTimes = -1,
                .toJoin = 0
            },
            Operation::Minus))
        {
            res.push_back(std::to_string(m_digits[pos]) + "-" + elt);
        }
        // TIMES
        // reconsile all joins
        for(auto& elt : getPossibilities(
            pos + 1, 
            {
                .toAdd = toAdd,
                .toTimes = toTimes * (m_digits[pos] + 10*toJoin),
                .toJoin = 0
            },
            Operation::Times))
        {
            res.push_back(std::to_string(m_digits[pos]) + "*" + elt);
        }
        // JOIN
        // reconsile nothing
        if(m_digits[pos] != 0 || priorOp == Operation::Join)
        for(auto& elt : getPossibilities(
            pos + 1, 
            {
                .toAdd = toAdd,
                .toTimes = toTimes,
                .toJoin = (m_digits[pos] + 10*toJoin),
            },
            Operation::Join))
        {
            res.push_back(std::to_string(m_digits[pos]) + elt);
        }

        return res;
    }
    
    vector<string> addOperators(string& num, int target) {
        m_digits.clear();
        auto todigit = [](char c) {return c - '0';};
        for(char c : num)
        {
            m_digits.push_back(todigit(c));
        }
        this->target = target;
        vector<string> res;
        for(auto& s : getPossibilities(
            1,
            {.toAdd = m_digits[0], .toTimes = 1, .toJoin = 0},
            Operation::Plus))
        {
            res.push_back(std::to_string(m_digits[0]) + "+" + s);
        }
        for(auto& s : getPossibilities(
            1,
            {.toAdd = m_digits[0], .toTimes = -1, .toJoin = 0},
            Operation::Minus))
        {
            res.push_back(std::to_string(m_digits[0]) + "-" + s);
        }
        for(auto& s : getPossibilities(
            1,
            {.toAdd = 0, .toTimes = m_digits[0], .toJoin = 0},
            Operation::Times))
        {
            res.push_back(std::to_string(m_digits[0]) + "*" + s);
        }
        if(m_digits[0] != 0)
        for(auto& s : getPossibilities(
            1,
            {.toAdd = 0, .toTimes = 1, .toJoin = m_digits[0]},
            Operation::Join))
        {
            res.push_back(std::to_string(m_digits[0]) + s);
        }
        return res;
    }
};
22203