text consists of lowercase English letters and round brackets. The brackets pair up in the usual way: each ) closes the nearest ( to its left that is still open.
Some brackets end up alone: a ) with no open ( before it, or a ( that nothing ever closes. Remove all such brackets and return what is left. Letters stay, and the order of characters does not change.
The result is always balanced (ignoring letters), and it is reached by removing the fewest brackets possible.text="f(o(o)b)a)r" → 'f(o(o)b)ar'
The last ) closes nothing that was opened, so it goes. Every other bracket has a partner.
text=")x(y" → 'xy'
The first ) has no opener to its left and the ( has no closer to its right, so both are removed.
text="((ab)" → '(ab)'
The second ( pairs with the ), leaving the first ( without a partner.
text contains only lowercase English letters, ( and )text="f(o(o)b)a)r"
'f(o(o)b)ar'
Run checks the first 3 tests in your browser. Submit checks all 79 tests on the server, hidden ones included.