Community Problem

Remove Outermost Parentheses

Difficulty: Easy

Strip the outer parentheses of every primitive group in a balanced parenthesis string, in one pass with a depth counter.

Remove Outermost Parentheses

Strip the outer parentheses of every primitive group in a balanced parenthesis string, in one pass with a depth counter.

EASY
Free
stack
strings
carlosherrera

By @carlosherrera

March 24, 2026

·

Updated May 18, 2026

715 views

19

Rate

I keep this one in my warmup rotation because the depth-counter trick is the same idea that powers the harder Decode String and Score of Parentheses problems, but stripped down to a single integer of state. The version interviewers ask is always "do it in one pass without building any auxiliary structures".

Remove Outermost Parentheses

A balanced parenthesis string can be uniquely decomposed into a concatenation of primitive groups, where a primitive group is a non-empty balanced parenthesis string that cannot itself be split into two non-empty balanced parts. Given a balanced string s, return the string formed by concatenating all primitive groups after stripping their outermost pair of parentheses.

For example, "(()())(())" decomposes as "(()())" plus "(())". After stripping outer parens you get "()()" plus "()", concatenated to "()()()".

Examples

Example 1:

  • Input: s = "(()())(())"
  • Output: "()()()"
  • Explanation: Primitives are "(()())" and "(())". Strip the outer pair of each to get "()()" and "()", then concatenate.

Example 2:

  • Input: s = "(()())(())(()(()))"
  • Output: "()()()()(())"
  • Explanation: Primitives are "(()())", "(())", "(()(()))". After stripping: "()()", "()", "()(())".

Example 3:

  • Input: s = "()()"
  • Output: ""
  • Explanation: Two primitives "()" and "()", both strip to empty.

Example 4:

  • Input: s = "()"
  • Output: ""
  • Explanation: One primitive that strips to empty.

Constraints

  • 1 <= s.length <= 10^5
  • s[i] is either '(' or ')'.
  • s is a non-empty balanced parenthesis string.

Follow-up

Can you do it in one pass and O(n) total work, without using a stack of characters?

Solution

Hints

0/4
Hint 1
Hint 2
Hint 3
Hint 4
All Problems