Reverse Every Parenthesized Segment From Inside Out

Quick Overview

Reverse Every Parenthesized Segment From Inside Out. The balanced string s contains lowercase letters and parentheses.

Reverse Every Parenthesized Segment From Inside Out

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem The balanced string s contains lowercase letters and parentheses. Reverse the text inside each matching pair, beginning with inner pairs. Remove all parentheses from the final result and return the remaining letters. ```cpp class Solution { public: string reverseParentheses(string s); }; ``` ### Examples **Example 1:** ```text Input: s = "a(bc)d" Output: "acbd" Explanation: Reversing the inner bc changes it to cb, leaving outside a and d in place. ``` **Example 2:** ```text Input: s = "(a(bc)d)" Output: "dbca" Explanation: First bc becomes cb; then the enclosing acbd reverses to dbca. ``` ### Constraints - s has length 1 to 2000. - It contains only lowercase English letters and parentheses. - All parentheses are balanced. ### Contract considerations The result contains letters only, and matching pairs are supplied by balanced syntax. ```hint Read the boundary condition Do nested pairs reverse the original interior or the text after its inner reversals, and may any parentheses remain? ``` ### Boundary reminders The result contains letters only, and matching pairs are supplied by balanced syntax.

Overview: Reverse Every Parenthesized Segment From Inside Out. The balanced string s contains lowercase letters and parentheses.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

The balanced string s contains lowercase letters and parentheses. Reverse the text inside each matching pair, beginning with inner pairs. Remove all parentheses from the final result and return the remaining letters.

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

Examples

Example 1:

Input: s = "a(bc)d"
Output: "acbd"
Explanation: Reversing the inner bc changes it to cb, leaving outside a and d in place.

Example 2:

Input: s = "(a(bc)d)"
Output: "dbca"
Explanation: First bc becomes cb; then the enclosing acbd reverses to dbca.

Constraints

  • s has length 1 to 2000.
  • It contains only lowercase English letters and parentheses.
  • All parentheses are balanced.

Contract considerations

The result contains letters only, and matching pairs are supplied by balanced syntax.

Boundary reminders

The result contains letters only, and matching pairs are supplied by balanced syntax.