#1021

Remove Outermost Parentheses

newbie · 145 · lc easy +13 · verified · 86.9% accepted · 3,722 likes · top 98%

Description

A valid parentheses string s is primitive if it is non-empty and cannot be written as the concatenation of two non-empty valid parentheses strings.

Every valid parentheses string has a unique primitive decomposition s = P1 + P2 + ... + Pk.

Return s after stripping the outermost parentheses from each primitive part.

Example 1:

Input: s = "(()())(())"
Output: "()()()"
Explanation:
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".

Example 2:

Input: s = "(()())(())(()(()))"
Output: "()()()()(())"
Explanation:
The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".

Example 3:

Input: s = "()()"
Output: ""
Explanation:
The input string is "()()", with primitive decomposition "()" + "()".
After removing outer parentheses of each part, this is "" + "" = "".

Code

1
2
3