Remove One Outer Pair from Every Primitive Parentheses Component

Quick Overview

Remove One Outer Pair from Every Primitive Parentheses Component. A valid parentheses string is empty, a matching pair around a valid string, or a concatenation of valid strings.

Remove One Outer Pair from Every Primitive Parentheses Component

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem A valid parentheses string is empty, a matching pair around a valid string, or a concatenation of valid strings. A nonempty valid string is primitive when it cannot split into two nonempty valid strings. Given valid `s`, decompose it into primitive components and remove each component's outermost pair. Return their remaining interiors concatenated in order. ```cpp class Solution { public: string removeOuterParentheses(string s); }; ``` ### Examples **Example 1:** ```text Input: s = "((()))()" Output: "(())" Explanation: The primitive components are((())) and(); their interiors are(()) and the empty string. ``` **Example 2:** ```text Input: s = "(())(())" Output: "()()" Explanation: Each primitive component loses its outer pair and contributes(). ``` ### Constraints - `1 <= s.length <= 10^5`. - Every character is `(` or `)`. - s is a valid parentheses string. ### Contract considerations The input may consist of many primitive components, and each loses its own outer pair. ```hint Read the boundary condition Depth returning to zero identifies the boundary between primitive components. ``` ### Boundary reminders The input may consist of many primitive components, and each loses its own outer pair.

Overview: Remove One Outer Pair from Every Primitive Parentheses Component. A valid parentheses string is empty, a matching pair around a valid string, or a concatenation of valid strings.

Amazon logo
Amazon
Aug 29, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

A valid parentheses string is empty, a matching pair around a valid string, or a concatenation of valid strings. A nonempty valid string is primitive when it cannot split into two nonempty valid strings. Given valid s, decompose it into primitive components and remove each component's outermost pair. Return their remaining interiors concatenated in order.

class Solution {
public:
    string removeOuterParentheses(string s);
};

Examples

Example 1:

Input: s = "((()))()"
Output: "(())"
Explanation: The primitive components are((())) and(); their interiors are(()) and the empty string.

Example 2:

Input: s = "(())(())"
Output: "()()"
Explanation: Each primitive component loses its outer pair and contributes().

Constraints

  • 1 <= s.length <= 10^5 .
  • Every character is ( or ) .
  • s is a valid parentheses string.

Contract considerations

The input may consist of many primitive components, and each loses its own outer pair.

Boundary reminders

The input may consist of many primitive components, and each loses its own outer pair.