Ctrl K

Peel the Outer Brackets

Easy
You are given a balanced bracket string s made of ( and ): every opening bracket has a matching closing one after it. Cut s into top-level blocks, the shortest non-empty pieces that are balanced on their own. In other words, a block ends wherever the number of open brackets drops back to zero. For example, (()())(()) consists of the blocks (()()) and (()). Strip the first and last bracket from every block and return what is left, joined together in the original order.
Example 1
s='(())((()))' → '()(())'

The blocks are "(())" and "((()))". Without their outer brackets they become "()" and "(())", which join into "()(())".

Example 2
s='()()()' → ''

Three "()" blocks, each empty inside, so the result is an empty string.

Example 3
s='(()()())' → '()()()'

The whole string is one block; removing its outer pair leaves "()()()".

Constraints
  • 1 ≤ s.length ≤ 10⁵
  • s[i] is ( or )
  • s is a balanced bracket string
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBracket Balance EasySame Text After Erasing EasyPopping Pairs EasyCase Pairs Cancel Out EasySteps Back to the Top Folder Easy
loading Python…
Input
s='(())((()))'
Expected
'()(())'

Run checks the first 3 tests in your browser. Submit checks all 104 tests on the server, hidden ones included.