Given a string s of ( and ) characters, return the minimum number of parentheses to add to make the string valid (every opening has a matching closing and vice versa).
Track unmatched open and unmatched close parentheses. Scan left to right: increment open count for (, for ) either decrement open if open>0 or increment close count.
- open = unmatched open brackets, close = unmatched close brackets.
- For each char: if ( increment open.
- If ): if open > 0, decrement open (matched); else increment close.
- Answer = open + close.
- Time Complexity: O(N)
- Space Complexity: O(1)