Skip to content
Veltoryx Technologies
ProProblem 59● MediumLeetCode #921 ↗4 min readUpdated 30 October 2026

Minimum Add to Make Parentheses Valid

Calculate the minimum number of parentheses to insert to make a string valid using balance counters in O(1) auxiliary space.

01The problem

A parentheses string is valid if and only if:

  1. It is the empty string,
  2. It can be written as AB (A concatenated with B), where A and B are valid strings, or
  3. It can be written as (A), where A is a valid string.

You are given a parentheses string s. In one move, you can insert a parenthesis at any position of the string.

Return the minimum number of moves required to make s valid.

InputOutputWhy
"())"1One '(' inserted at the beginning makes it "()()"
"((("3Three ')' inserted at the end make it "((()))"
"()"0Already valid; 0 insertions needed

Constraints: 1 <= s.length <= 1000, s[i] is either '(' or ')'.

Loading the rest of the lesson…