Minimum Add to Make Parentheses Valid
Given a string s of parentheses, return the minimum number of parentheses (either '(' or ')') you must insert so that the string becomes valid. A string is valid when every opening parenthesis has a matching closing parenthesis in the correct order and vice versa.
Open official problem prompt ↗Count the fewest single-character parenthesis insertions that make the whole string balanced.
Like reconciling a ledger of matched IOUs: each '(' is an open debt and each ')' pays one off; a payment with no debt to cover and any debts left unpaid at close each cost one correction.
- Input
- s = "())"
- Output
- 1
- Why
- One '(' inserted (e.g. "(())") matches the extra ')', making the string valid with a single addition.
1 <= s.length <= 1000s consists only of the characters '(' and ')'