<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 {
public:
    int calculate(string s) {
        
        // Is it safe to assume that s is a valid expression
        // S contains , + , - , * , / -> numbers and spaces
        // Is the number going to fit within 32- bit integer.
        
        // So I think we have some idea on what will be the inputs
        // 3 + 2 * 5 / 6 + 3 - 4
        // 5 * 7 - 3
        // 3 - 6 / 2
        // So we can maintain a stack of int which stores the +/- operation that would be required later.
        
        // +3, +2 *5
        int N = s.size();
        int current = 0; 
        char currentSign = '+';
        stack<pair<char, int>> holdValues;
        
        for(int i=0; i<N; i++){
            
            if(s[i] == ' '){
                continue;
            }else if(s[i] >= '0' && s[i] <= '9'){
                                    
                current = current*10 + (s[i]-'0');
            }else{
                
                pushValues(holdValues, currentSign, current);
                
                current = 0;
                currentSign = s[i];
            }
            
        }
        
        pushValues(holdValues, currentSign, current);
        
        int answer = 0;
        
        while(!holdValues.empty()){
            pair<char, int> topValue = holdValues.top();
            holdValues.pop();
            
            answer += (topValue.first == '+') ? topValue.second : -topValue.second;
        }
        
        return answer;
        
    }
    
private:
    void pushValues(stack<pair<char,int>>& holdValues, char sign, int number){
        
        if(holdValues.empty() || sign == '+' || sign == '-'){
            holdValues.push({sign, number});
            return;
        }
            
        pair<char, int> topValue = holdValues.top();
        holdValues.pop();
        if(sign == '*'){
            holdValues.push({topValue.first, topValue.second* number});
        }else{
            holdValues.push({topValue.first, topValue.second/number});
        }
        
    }
};
16613