Back to Month
MEDIUM 27 Sep 2026 View on LeetCode

1190. Reverse Substrings Between Each Pair of Parentheses

</> Solution

class Solution {
    public String reverseParentheses(String s) {
        Deque<StringBuilder> stack = new ArrayDeque<>();
        StringBuilder current = new StringBuilder();
        for (char c : s.toCharArray()) {
            if (c == '(') {
                stack.push(current);
                current = new StringBuilder();
            } else if (c == ')') {
                current.reverse();
                current = stack.pop().append(current);
            } else {
                current.append(c);
            }
        }
        return current.toString();
    }
}

TIME COMPLEXITY

O(n²)

SPACE COMPLEXITY

O(n)

TOPICS

Stack String