1190. Reverse Substrings Between Each Pair of Parentheses

Brute Force

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

        for (char c : s)
        {
            if (c == '(')
            {
                stk.push(res.size());
            }
            else if (c == ')')
            {
                int start = stk.top(); stk.pop();
                reverse(res.begin() + start, res.end());
            }
            else
            {
                res += c;
            }
        }
        return res;
    }
};
  • T: O(N2)O(N^2)
  • S: O(N)O(N)