<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p>

<p><strong>Note:</strong> You are <strong>not</strong> 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 = &quot;1 + 1&quot;
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot; 2-1 + 2 &quot;
<strong>Output:</strong> 3
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;(1+(4+5+2)-3)+(6+8)&quot;
<strong>Output:</strong> 23
</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 digits, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, and <code>&#39; &#39;</code>.</li>
	<li><code>s</code> represents a valid expression.</li>
	<li><code>&#39;+&#39;</code> is <strong>not</strong> used as a unary operation (i.e., <code>&quot;+1&quot;</code> and <code>&quot;+(2 + 3)&quot;</code> is invalid).</li>
	<li><code>&#39;-&#39;</code> could be used as a unary operation (i.e., <code>&quot;-1&quot;</code> and <code>&quot;-(2 + 3)&quot;</code> is valid).</li>
	<li>There will be no two consecutive operators in the input.</li>
	<li>Every number and running calculation will fit in a signed 32-bit integer.</li>
</ul>

class Solution {
    bool isdigit(char ch) {
        if(ch >= '0' && ch <= '9')
            return true;
        return false;
    }

    string convertCharToString(char ch) {
        string dummy = "";
        dummy = dummy + ch;
        return dummy;
    }

public:
    int calculate(string str) {
        str = "(" + str + ")";
        reverse(str.begin(), str.end());
        stack<string> st;
        for(int i=0;i<str.length();i++) {
            // cout << "currently processing char : " << str[i] << endl;
            if(str[i] == ')' || isdigit(str[i]) || str[i] == '+' || str[i] == '-' ) {
                st.push(convertCharToString(str[i]));
            }
            else if (str[i] == '('){
                while(1) {
                    int key;
                    if(st.top() == "-") {
                        key = 0;
                    }
                    else {
                        key = stoi(st.top()); 
                        st.pop();
                        while(st.top() != ")" && st.top() != "+" && st.top() != "-") {
                            key = key * 10 + stoi(st.top());
                            st.pop();
                        }                        
                    }
                    // cout << "key popped is " << key << endl;
                    string op = st.top(); st.pop();
                    if(op == ")") {
                        st.push(to_string(key));
                        st.push(op);
                        break;
                    }
                    int key2 = stoi(st.top()); st.pop();
                    while(st.top()[0] != ')' && st.top()[0] != '+' && st.top()[0] != '-') {
                        key2 = key2 * 10 + stoi(st.top());
                        st.pop();
                    }

                    // cout << "key2 popped is " << key2 << endl;
                    // cout << "performing operation : " << key << op << key2 << endl;
                    int res = (op == "+") ? key + key2 : key - key2;
                    st.push(to_string(res));
                }
                st.pop();
            }
        }
        return stoi(st.top());
    }
};
29993