You are given a string s that consists of lower case English letters and brackets. 
Reverse the strings in each pair of matching parentheses, starting from the innermost one.
Your result should not contain any brackets.
 
Example 1:
Input: s = "(abcd)"
Output: "dcba"

Example 2:
Input: s = "(u(love)i)"
Output: "iloveu"
Explanation: The substring "love" is reversed first, then the whole string is reversed.

Example 3:
Input: s = "(ed(et(oc))el)"
Output: "leetcode"
Explanation: First, we reverse the substring "oc", then "etco", and finally, the whole string.

Example 4:
Input: s = "a(bcdefghijkl(mno)p)q"
Output: "apmnolkjihgfedcbq"

 
Constraints:

0 <= s.length <= 2000
s only contains lower case English characters and parentheses.
It's guaranteed that all parentheses are balanced.

Difficulty: interview

Starter code:

class Solution:
    def reverseParentheses(self, s: str) -> str:
        

Solutions:
class Solution:
    def reverseParentheses(self, s: str) -> str:
        
        
        stack = []
        curr = ''
        for c in s:
            if c=='(':
                stack.append(curr)
                curr = ''
                stack.append('(')
            elif c==')':
                stack.append(curr)
                curr = ''
                aux = ''
                while stack and stack[-1]!='(':
                    aux=stack.pop()+aux
                stack.pop()
                stack.append(aux[::-1])
            else:
                curr+=c
        if curr:
            stack.append(curr)
        return ''.join(stack)

class Solution:
    def reverseParentheses(self, s: str) -> str:
        stack = []
        for i in s:
            if i != ')':
                stack.append(i)
            else:
                temp = []
                while stack and stack[-1] != '(':
                    temp.append(stack.pop())
                if stack[-1] == '(':
                    stack.pop()
                for k in temp:
                    stack.append(k)
        return ''.join(stack)


class Solution:
    # https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/383670/JavaC++Python-Why-not-O(N)
    def reverseParentheses(self, s: str) -> str:
        stack, pairs = [], {}
        for i, c in enumerate(s):
            if c == '(':
                stack.append(i)
            elif c == ')':
                j = stack.pop()
                pairs[i], pairs[j] = j, i
        i, d, res = 0, 1, []
        while i < len(s):
            if s[i] in '()':
                i = pairs[i]
                d = -d
            else:
                res.append(s[i])
            i += d
        return ''.join(res)
    
    # O(n^2)
    def reverseParentheses1(self, s: str) -> str:
        stack = []
        for c in s:
            if c == ')':
                chars = []
                while stack and stack[-1] != '(':
                    chars.append(stack.pop())
                stack.pop()
                for ch in chars:
                    stack.append(ch)
            else:
                stack.append(c)
        # e.g. \"a(bcdefghijkl(mno)p)q\"
        return ''.join(stack)


class Solution:
    def reverseParentheses(self, s: str) -> str:
        opened = []
        pair = {}
        for i, c in enumerate(s):
            if c == '(':
                opened.append(i)
            if c == ')':
                j = opened.pop()
                pair[i], pair[j] = j, i
        res = []
        i, d = 0, 1
        while i < len(s):
            if s[i] in '()':
                i = pair[i]
                d = -d
            else:
                res.append(s[i])
            i += d
        return ''.join(res)


class Solution:
    def reverseParentheses(self, s: str) -> str:
        stack = ['']
        for c in s:
            if c == '(':
                stack.append('')
            elif c == ')':
                word = stack.pop()[::-1]
                stack[-1] += word
            else:
                stack[-1] += c
        return ''.join(stack)

class Solution:
    def reverseParentheses(self, s: str) -> str:
        stack = []
        for i in s:
            if i == ')':
                temp = []
                while stack and stack[-1] != '(':
                    temp.append(stack.pop())
                if stack:
                    stack.pop()
                for k in temp:
                    stack.append(k)
            else:
                 stack.append(i)
        return ''.join(stack)


class Solution:
    def reverseParentheses(self, s: str) -> str:
        stack = ['']
        
        for char in s:
            if char == '(':
                stack.append('')
            elif char == ')':
                temp = stack.pop()[::-1]
                stack[-1] += temp
            else:
                stack[-1] += char
        
        return ''.join(stack)