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.s='(())((()))' → '()(())'
The blocks are "(())" and "((()))". Without their outer brackets they become "()" and "(())", which join into "()(())".
s='()()()' → ''
Three "()" blocks, each empty inside, so the result is an empty string.
s='(()()())' → '()()()'
The whole string is one block; removing its outer pair leaves "()()()".
s.length ≤ 10⁵s[i] is ( or )s is a balanced bracket strings='(())((()))'
'()(())'
Run checks the first 3 tests in your browser. Submit checks all 104 tests on the server, hidden ones included.