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

def postfixEvaluation(tokens):
    stack = []
    for char in tokens:
        if char == "+":
            op1 = stack.pop()
            op2 = stack.pop()
            stack.append(op2 + op1)
        elif char == "-":
            op1 = stack.pop()
            op2 = stack.pop()
            stack.append(op2 - op1)
        elif char == "*":
            op1 = stack.pop()
            op2 = stack.pop()
            stack.append(op2 * op1)
        elif char == "/":
            op1 = stack.pop()
            op2 = stack.pop()
            stack.append(int(op2 / op1))  # Ensure integer division
        elif char == "neg":
            op1 = stack.pop()
            stack.append(-op1)  # Apply unary negation
        else:
            # If it's a number, append it to the stack
            stack.append(int(char))  
    
    return stack.pop()

def infixToPostfix(s):
    operators = {'+', '-', '*', '/'}
    precedence_stack = {'(': 0, '+': 1, '-': 1, '*': 2, '/': 2, 'neg': 3}
    precedence_input = {'(': 4, '+': 1, '-': 1, '*': 2, '/': 2, 'neg': 3, ')': 0}
    output = []
    # Wrap `s` with '(' and ')' to make processing easier
    s = s + ')'
    stack = ['(']
    prevC = '('

    hasNum = False
    num = 0
    for c in s:
        if c == ' ':
            continue

        # c is part of a number
        if c.isdigit():
            hasNum = True
            num = num * 10 + int(c)
            prevC = c
            continue

        # c is an operator
        if hasNum:
            hasNum = False
            output.append(str(num))  # Convert number to string for easier evaluation
            num = 0

        # Check if it's a negative unary operator
        if c == '-' and (prevC == '(' or prevC in operators):
            c = 'neg'

        # Pop until '('
        while precedence_stack[stack[-1]] >= precedence_input[c]:
            popped = stack.pop()
            if popped != '(':
                output.append(popped)
            else:
                break

        # Push input onto the stack
        if c != ')':
            stack.append(c)

        prevC = c

    return output

def fun(s):
    postfixExp = infixToPostfix(s)  # Convert infix to postfix
    result = postfixEvaluation(postfixExp)  # Evaluate the postfix expression
    return result
    
class Solution:
    def calculate(self, s: str) -> int:
        return fun(s)

25888