<reponame>YiqunPeng/leetcode_pro<filename>solutions/1190_reverse_substrings_between_each_pair_of_parentheses.py<gh_stars>0
class Solution:
    def reverseParentheses(self, s: str) -> str:
        """Stack.

        Running time: O(n^2) where n is the length of s.
        """
        st = []
        
        for c in s:
            if c == '(':
                st.append('')
            elif c == ')':
                v = st.pop()[::-1]
                if st:
                    st[-1] += v
                else:
                    st.append(v)
            else:
                if st:
                    st[-1] += c
                else:
                    st.append(c)
        
        return st[0]
            
class Solution:
    def reverseParentheses(self, s: str) -> str:
        """Stack.

        Running time: O(n^2) where n is the length of s.
        """
        st = []
        
        for c in s:
            if c == '(':
                st.append('')
            elif c == ')':
                v = st.pop()[::-1]
                if st:
                    st[-1] += v
                else:
                    st.append(v)
            else:
                if st:
                    st[-1] += c
                else:
                    st.append(c)
        
        return st[0]
Stack.          Running time: O(n^2) where n is the length of s.