s made of valid ("primitive") parentheses, split it into primitives (each is a non-empty balanced block that cannot be split further) and return the concatenation of every primitive with its outermost pair of parentheses removed.Remove Outer Parentheses
L4 Easy Stack
Concept
For a string made of nested primitive parentheses, the outermost layer wraps each primitive. Track the depth to know which brackets belong to that outer shell.
Given a string
Examples
▸ s = "(()())(())"
→ ()()()
▸ s = "(()())(())(()(()))"
→ ()()()()(())
▸ s = "()()"
→
Progressive Hints
Hint 1 · Nudge
The outermost pair is the one at depth 0, so skip the characters that live at depth 0.
Hint 2 · Plan
Track a depth counter starting at 0. For '(': bump depth, and only copy it when depth is greater than 1. For ')': drop depth first, and only copy it when depth is still greater than 0. Join and return what you copied.
Hint 3 · Approach
depth = 0, output = []. For each c: if c == '(' then depth = depth + 1 and if depth > 1 append c; else depth = depth - 1 and if depth > 0 append c. Return output joined.
All hints are out. Take a breath and give it a shot.
Output
// Run your code to see the output here.