Problem 587
Remove Outermost Parentheses
Problem
A valid parentheses string is primitive if it is non-empty and cannot be split into two non-empty valid strings, like (()) or (). Every valid string splits uniquely into consecutive primitive parts. Remove the outermost pair of parentheses from each primitive part and print what remains, or EMPTY if nothing remains. Track the depth while scanning: keep an opening bracket only if the depth before it is at least 1, and a closing bracket only if the depth after it is at least 1.
Input
A single valid parentheses string S.
Output
Print the result, or EMPTY if it is empty.
Constraints
2 <= |S| <= 100000; S is a valid parentheses string
Example input
(()())(())
Example output
()()()
Explanation
(()())(()) splits into the primitive parts (()()) and (()). Removing the outer pair of each gives ()() and (), so the answer is ()()().