Back to October Back to Year 2026
EASY 08 Oct 2026 View on LeetCode

1021. Remove Outermost Parentheses

</> Solution

/**
 * Problem: LeetCode 1021 - Remove Outermost Parentheses
 * Difficulty: Easy
 * Language: Java
 * Author: CodeByTushu
 */
class Solution {
    public String removeOuterParentheses(String s) {
        StringBuilder sb = new StringBuilder();
        int depth = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(') {
                if (depth > 0) sb.append(c);
                depth++;
            } else {
                depth--;
                if (depth > 0) sb.append(c);
            }
        }
        return sb.toString();
    }
}

TIME COMPLEXITY

O(N) where N = s.length

SPACE COMPLEXITY

O(1)

TOPICS

Stack String