Skip to content
DeepForgeby svx
All problems
HardAlgorithmsal-300

Remove Outermost Parentheses

Problem

Remove the outermost pair of every primitive group in the balanced parentheses string s and return the concatenation of the inner parts.

Track the current nesting depth: append an opening bracket only when the depth before it is positive, and append a closing bracket only when the depth after it is positive. Assume s is a valid parentheses string.

Hint

A primitive group returns to depth zero exactly at its closing outer bracket.

Test cases

case 1
in: ["(()())(())"]
expected: "()()()"
case 2
in: ["(()())(())(()(()))"]
expected: "()()()()(())"
case 3
in: ["()()"]
expected: ""
case 4
in: ["(()())"]
expected: "()()"
case 5
in: [""]
expected: ""
Starter codePython
def remove_outer_parentheses(s):
    # Your code here
    pass

5 test cases · run your code against them in the editor