A festival organizer strings several independent paper-lantern clusters into one long display cable, one cluster immediately after another with no gaps. Along the cable, each character is either ( — a new lantern shell is opened around whatever is currently open — or ) — the innermost currently open shell is sealed. The whole cable is guaranteed to be a valid sequence (every opened shell is eventually sealed, and no shell is ever sealed before it is opened), and it was built by joining, end to end, one or more self-contained clusters: a cluster never returns to zero open shells before its very last character (equivalently, no cluster can be split into two shorter clusters that are each independently valid on their own).
For a decoration touch, the organizer wants to strip away just the single outermost shell of every cluster — that cluster's very first ( and its very last ) — while keeping every other character of that cluster in its original relative order, and then splice the leftovers of all the clusters back together, in order, into one string.
s consisting only of the characters ( and ).Print the resulting string after removing the outermost shell of every cluster (this may be an empty line if nothing is left).
s <= 100000s is evens is a valid parentheses string formed by concatenating one or more primitive (self-contained) valid parentheses stringsExample 1
Input
(())(()())
Expected
()()()
Explanation
The cable is made of two self-contained clusters: "(())" and "(()())". Stripping the outer shell of the first leaves "()"; stripping the outer shell of the second leaves "()()" . Splicing them together gives "()()()".
Example 2
Input
((()))
Expected
(())
Explanation
The entire cable is a single cluster nesting three shells deep. Stripping only its outermost shell (the very first '(' and the very last ')') leaves the inner four characters intact: "(())".
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →