1190 - Reverse Substrings Between Each Pair of Parentheses

 JAVA

  • class Solution {
        public String reverseParentheses(String s) {
            int n = s.length();
            int[] d = new int[n];
            Deque<Integer> stk = new ArrayDeque<>();
            for (int i = 0; i < n; ++i) {
                if (s.charAt(i) == '(') {
                    stk.push(i);
                } else if (s.charAt(i) == ')') {
                    int j = stk.pop();
                    d[i] = j;
                    d[j] = i;
                }
            }
            StringBuilder ans = new StringBuilder();
            int i = 0, x = 1;
            while (i < n) {
                if (s.charAt(i) == '(' || s.charAt(i) == ')') {
                    i = d[i];
                    x = -x;
                } else {
                    ans.append(s.charAt(i));
                }
                i += x;
            }
            return ans.toString();
        }
    }

C++

  • class Solution {
    public:
        string reverseParentheses(string s) {
            string stk;
            for (char& c : s) {
                if (c == ')') {
                    string t;
                    while (stk.back() != '(') {
                        t.push_back(stk.back());
                        stk.pop_back();
                    }
                    stk.pop_back();
                    stk += t;
                } else {
                    stk.push_back(c);
                }
            }
            return stk;
        }
    };
    

Comments