Skip to content

Practice Minimum Insertions to Balance a Parentheses String

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1541
LeetCode Problem

Minimum Insertions to Balance a Parentheses String

Problem Statement

Treat ( as an opening token and two consecutive characters )) as one closing token. Every opening must match a later closing token in a properly nested sequence. Insert individual ( or ) characters anywhere in s and return the minimum number of inserted characters needed to balance it.

Source: Minimum Insertions to Balance a Parentheses String on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "(()))"
Output
1

# Example 2

Input
s = "())"
Output
0

# Example 3

Input
s = "))())("
Output
3

Constraints

1 <= s.length <= 10^5
s consists of '(' and ')' only.

Related LeetCode Problems

Practice more problems similar to Minimum Insertions to Balance a Parentheses String.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.