A valid parentheses string is either empty "", "(" + A + ")", or A + B, where A and B are valid parentheses strings, and + represents string concatenation. For example, "", "()", "(())()", and "(()(()))" are all valid parentheses strings. A valid parentheses string s is primitive if it is nonempty, and there does not exist a way to split it into s = A + B, with A and B nonempty valid parentheses strings. Given a valid parentheses string s, consider its primitive decomposition: s = P_1 + P_2 + ... + P_k, where P_i are primitive valid parentheses strings. Return s after removing the outermost parentheses of every primitive string in the primitive decomposition of s.
Example 1
Input: s = (()())(())
Output: ()()()
Explanation: The string is two primitive pieces, "(()())" and "(())". Stripping the outer pair from each leaves "()()" and "()", which join to "()()()".
Example 2
Input: s = (()())(())(()(()))
Output: ()()()()(())
Explanation: Three primitives "(()())", "(())", and "(()(()))" become "()()", "()", and "()(())" once their outermost brackets are removed, giving "()()()()(())".
Example 3
Input: s = ()()
Output:
Explanation: Both primitives are just "()", so each loses its only pair and nothing is left, making the result empty.
Track the nesting depth as you scan. A bracket is 'outermost' exactly when it opens depth 0 or closes back to depth 0 — those are the ones to drop; keep everything in between.
Run checks all cases above. Submit evaluates all test cases.