Medium · Strings

Minimum Add to Make Parentheses Valid

Given a string s of only '(' and ')', return the minimum number of parentheses that must be inserted (at any positions) to make s a valid parentheses string.

Examples

Example 1

{
  "s": "())"
}

Output: 1 to add

Example 2

{
  "s": "((("
}

Output: 3 to add

Rebuild it in the studio

Core interview problems are free with a full runner and lessons; Pro opens the complete library.

More Strings problems