<p>You are given a string <code>s</code> that consists of lower case English letters and brackets.</p>

<p>Reverse the strings in each pair of matching parentheses, starting from the innermost one.</p>

<p>Your result should <strong>not</strong> contain any brackets.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;(abcd)&quot;
<strong>Output:</strong> &quot;dcba&quot;
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;(u(love)i)&quot;
<strong>Output:</strong> &quot;iloveu&quot;
<strong>Explanation:</strong> The substring &quot;love&quot; is reversed first, then the whole string is reversed.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;(ed(et(oc))el)&quot;
<strong>Output:</strong> &quot;leetcode&quot;
<strong>Explanation:</strong> First, we reverse the substring &quot;oc&quot;, then &quot;etco&quot;, and finally, the whole string.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 2000</code></li>
	<li><code>s</code> only contains lower case English characters and parentheses.</li>
	<li>It is guaranteed that all parentheses are balanced.</li>
</ul>

class Solution {
public:
    string reverseParentheses(string s) {
        
        int n = s.length();

        string t = string(n, '.');

        vector<int> stk;

        unordered_map<int,int> smp;
        unordered_map<int,int> emp;

        for( int i=0; i<n; i++) {
            if(s[i]=='(') {
                stk.push_back(i);
            } else if(s[i]==')') {
                smp[stk.back()] = i;
                emp[i] = stk.back();
                stk.pop_back();
            }
        }

        function<void(int,int,int,int)> solve;

        solve = [&]( int l, int dl, int fl, int fr)->void {
            while(fl<=fr) {
                if(dl==1) {
                    if(s[l]=='(') {
                        int sid = l;
                        int eid = smp[l];
                        t[fl] = '(';
                        t[fl+eid-sid] = ')';
                        solve( l+dl*(eid-sid)+dl*(-1), dl*(-1), fl+1, fl+eid-sid-1);
                        l += dl*(eid-sid+1);
                        fl += (eid-sid+1);
                    } else {
                        t[fl] = s[l];
                        fl += 1;
                        l += dl;
                        // cout << t << endl;
                    }
                } else {
                    if(s[l]==')') {
                        int sid = emp[l];
                        int eid = l;
                        t[fl] = '(';
                        t[fl+eid-sid] = ')';
                        // cout << t << endl;
                        solve( l+dl*(eid-sid)+dl*(-1), dl*(-1), fl+1, fl+eid-sid-1);
                        l += dl*(eid-sid+1);
                        fl += (eid-sid+1);
                    } else {
                        t[fl] = s[l];
                        fl += 1;
                        l += dl;
                        // cout << t << endl;
                    }
                }
            }
        };

        solve( 0, 1, 0, n-1);

        string ans = "";
        for( char ch: t) {
            if((ch!='(') && (ch!=')')) {
                ans.push_back(ch);
            }
        }

        return ans;
    }
};
8800