<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.size();

        stack<int> openParenthesesIndices;
        unordered_map<int, int> parenthesesPair;

        // First pass: Pair up parentheses
        for (int i = 0; i < n; i++)
        {
            if (s[i] == '(')
            {
                openParenthesesIndices.push(i);
            }
            else if (s[i] == ')')
            {
                int j = openParenthesesIndices.top(); openParenthesesIndices.pop();
                parenthesesPair[i] = j;
                parenthesesPair[j] = i;
            }
        }

        // Second pass: Build the result string
        string res;
        int currentIdx = 0;
        int direction = 1;

        while (currentIdx < n)
        {
            if (s[currentIdx] == '(' || s[currentIdx] == ')')
            {
                currentIdx = parenthesesPair[currentIdx];
                direction = -direction;
            }
            else
            {
                res.push_back(s[currentIdx]);
            }

            currentIdx += direction;
        }

        return res;
    }
};

// class Solution 
// {
// public:
//     string reverseParentheses(string s) 
//     {
//         return recursive(s, 0).first;
//     }

//     pair<string, int> recursive(string& s, int i)
//     {
//         string res;

//         while (i < s.size())
//         {
//             // meet sub-string
//             if (s[i] == '(')
//             {
//                 auto subRes = recursive(s, i + 1);
//                 res += subRes.first;
//                 i = subRes.second;
//             }
//             // finish current string itself
//             else if (s[i] == ')')
//             {
//                 reverse(res.begin(), res.end());
//                 return {res, i};
//             }
//             else
//             {
//                 res.push_back(s[i]);
//             }

//             i++;
//         }

//         return {res, res.size()};
//     }
// };

// class Solution 
// {
// public:
//     string reverseParentheses(string s) 
//     {
//         stack<int> st;
//         string res;

//         for (char& c : s)
//         {
//             if (c == '(')
//             {
//                 // Store the current length as the start index for future
//                 // reversal
//                 st.push(res.size());
//             }
//             else if (c == ')')
//             {
//                 int openSignPosition = st.top(); st.pop();
//                 reverse(res.begin() + openSignPosition, res.end());
//                 // Reverse the substring between the matching parentheses
//             }
//             else
//             {
//                 // Append non-parenthesis characters to the processed string
//                 res.push_back(c);
//             }
//         }

//         return res;
//     }
// };
8300