# time(N^2)   space O(N) 
class Solution:
    def calculate(self, s: str) -> int:
        def helper(s):
            st = []
            num = 0
            sign = '+'
            while s :
                c = s.pop(0)
                # 1. regular count number  
                if c.isdigit():
                    num = 10*num + int(c)
                # 2. check if   (
                if c == '(':
                    num = helper(s)

                # 3. do +-*/ operations with edge case ' '  and  reach the end 
                if(not c.isdigit() and c!=' ') or len(s) == 0 :
                    if sign == '+':
                        st.append(num)
                    elif sign =='-':
                        st.append(-num)
                    elif sign == '*':
                        st[-1] = st[-1] * num
                    elif sign =='/':
                        st[-1] = int(st[-1]/float(num))
                    num = 0
                    sign = c
                
                #4. check ) 
                if c == ')':
                    break
            return sum(st)
        return helper(list(s))

optimization: reverse list s so we just need to call pop() instead of pop(0)