Fewest Insertions When Each Opening Requires a Consecutive Closing Pair

Quick Overview

Fewest Insertions When Each Opening Requires a Consecutive Closing Pair. A balanced string is empty,a concatenation of balanced strings,or "("+A+"))" for balanced A.

Fewest Insertions When Each Opening Requires a Consecutive Closing Pair

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem A balanced string is empty,a concatenation of balanced strings,or "("+A+"))" for balanced A. Thus each opening parenthesis must be matched by two consecutive closing parentheses after it,and every closing parenthesis belongs to one pair. Insert either parenthesis anywhere to balance s with the fewest insertions. Return that minimum count. ```cpp class Solution { public: int minInsertions(string s); }; ``` ### Examples **Example 1:** ```text Input: s = "(" Output: 2 Explanation: Append two closing parentheses to obtain()). ``` **Example 2:** ```text Input: s = ")(" Output: 4 Explanation: Insert an opening before the first closing and another closing beside it,then append two closings for the final opening,to obtain "())())",two balanced blocks "())" followed by "())". Four insertions are necessary: every balanced string has length divisible by3,so with two original characters the only smaller possible completed length is3,and no length3 balanced string contains ")(" in that order. ``` ### Constraints - s has length 1 to 100000. - Every character is either an opening or closing parenthesis. ### Contract considerations The empty balanced string is part of the grammar even though the input is nonempty. ```hint Read the boundary condition Each opening needs a consecutive pair of closing parentheses. Include unfinished pairs and leading closing parentheses; only insertions are allowed. ``` ### Boundary reminders The empty balanced string is part of the grammar even though the input is nonempty.

Overview: Fewest Insertions When Each Opening Requires a Consecutive Closing Pair. A balanced string is empty,a concatenation of balanced strings,or "("+A+"))" for balanced A.

Google logo
Google
Aug 16, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

A balanced string is empty,a concatenation of balanced strings,or "("+A+"))" for balanced A. Thus each opening parenthesis must be matched by two consecutive closing parentheses after it,and every closing parenthesis belongs to one pair. Insert either parenthesis anywhere to balance s with the fewest insertions. Return that minimum count.

class Solution {
public:
    int minInsertions(string s);
};

Examples

Example 1:

Input: s = "("
Output: 2
Explanation: Append two closing parentheses to obtain()).

Example 2:

Input: s = ")("
Output: 4
Explanation: Insert an opening before the first closing and another closing beside it,then append two closings for the final opening,to obtain "())())",two balanced blocks "())" followed by "())". Four insertions are necessary: every balanced string has length divisible by3,so with two original characters the only smaller possible completed length is3,and no length3 balanced string contains ")(" in that order.

Constraints

  • s has length 1 to 100000.
  • Every character is either an opening or closing parenthesis.

Contract considerations

The empty balanced string is part of the grammar even though the input is nonempty.

Boundary reminders

The empty balanced string is part of the grammar even though the input is nonempty.