<p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>.&nbsp;</p>

<p>The integer division should truncate toward zero.</p>

<p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>.</p>

<p><strong>Note:</strong> You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "3+2*2"
<strong>Output:</strong> 7
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = " 3/2 "
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> s = " 3+5 / 2 "
<strong>Output:</strong> 5
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li>
	<li><code>s</code> consists of integers and operators <code>(&#39;+&#39;, &#39;-&#39;, &#39;*&#39;, &#39;/&#39;)</code> separated by some number of spaces.</li>
	<li><code>s</code> represents <strong>a valid expression</strong>.</li>
	<li>All the integers in the expression are non-negative integers in the range <code>[0, 2<sup>31</sup> - 1]</code>.</li>
	<li>The answer is <strong>guaranteed</strong> to fit in a <strong>32-bit integer</strong>.</li>
</ul>

class Solution {

    std::vector<variant<int, char>> parseString(string s) {
        int n = s.size();
        
        std:string number_string;
        std::vector<variant<int, char>> number_and_symbols;
         
        for (int i{0}; i < n; ++i) {
            if (s[i] == ' ') continue;
            char c_shifted = s[i] - '0';
            if (c_shifted >= 0 && c_shifted < 10) {
                number_string += s[i];
                continue;
            }
            // At this point this is a symbol check if we need to add a number
            if (number_string.size() > 0) {
                number_and_symbols.push_back(stoi(number_string));
                number_string.clear();
            } 
            number_and_symbols.push_back(s[i]);
        }

        if (number_string.size() > 0) {
            number_and_symbols.push_back(stoi(number_string));
            number_string.clear();
        } 
        return number_and_symbols;
    }
    
    void print_vector(const vector<variant<int, char>>& values) {
        for (int i{0}; i < values.size(); ++i) {
            if (holds_alternative<int>(values[i])) {
                std::cout << get<int>(values[i]) << " ";
            }
            if (holds_alternative<char>(values[i])) {
                std::cout << get<char>(values[i]) << " ";
            }
        }
        std::cout << std::endl;
    }
    
    int do_operation(int lhs, int rhs, char operation) {
        switch (operation) {
            case '*':
                return lhs * rhs;
            case '-':
                return lhs - rhs;
            case '/':
                return lhs / rhs;
            case '+':
                return lhs + rhs;
        }
        return 0;
    }

    std::vector<variant<int, char>> reduce(const std::vector<variant<int, char>>& values, char operand1, char operand2) {
        // If it is a number then add parse 
        std::vector<variant<int, char>> results;
        optional<char> last_symbol;
        for (int i{0}; i < values.size(); ++i) {
            if (holds_alternative<int>(values[i])) {
                int n = get<int>(values[i]);
                if (last_symbol.has_value() && last_symbol.value() == operand1) {
                    int last_number = get<int>(results.back());
                    results.back() = do_operation(last_number, n, operand1);
                    last_symbol.reset();
                    continue;
                }
                if (last_symbol.has_value() && last_symbol.value() == operand2) {
                    int last_number = get<int>(results.back());
                    results.back() = do_operation(last_number, n, operand2);
                    last_symbol.reset();
                    continue;
                } 
                
                // If last symbol is empty just add the value to results
                results.push_back(n);
            } else {
                // this is a symbol so either add it to last symbol or add it to results
                char c = get<char>(values[i]);
                if (c == operand1 || c == operand2) {
                    last_symbol = c;
                } else {
                    results.push_back(c);
                }
            }
        }
        return results;
    }
    
public:
    int calculate(string s) {
        int n = s.size();
        
        const auto values = parseString(s);
        // print_vector(values);
        const auto results1 = reduce(values, '*', '/');
        const auto results2 = reduce(results1, '+', '-');
        // print_vector(results1);
        // print_vector(results2);
        return get<int>(results2.back());
    }
    
    // traverse:
    //      skip if empty
    //      if number add to number stack
    //      if symbol then grab previous number
    //      we can build a tree of expressions and make sure that we do multiplications and divisions first
    //      after we do all multiplications and divitions then lets do sums and substractions
     
};
32335