Minimum Add to Make Parentheses Valid

Medium

Description

Given a string of parentheses, return the minimum number of parentheses you must add to make it valid.

Examples

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

Add one '(' at start.

Input:s = "((("
Output:3
Explanation:

Need to add 3 closing parentheses ')' to match the 3 opening parentheses '('

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

Need to add 1 opening parenthesis '(' at the beginning to match the first ')' and 2 closing parentheses '))' to match the last two '((' characters

Constraints

  • 1 ≤ s.length ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!