You are given a string s consisting only of characters '(' and ')'.
Repeatedly remove any matched pair of parentheses as part of a valid parentheses matching (equivalently, cancel out all valid matches). After all possible matches are removed, return the number of remaining characters (the count of unmatched parentheses).
s = "()"
→ all matched → return
0
s = "())"
→ one pair matched, one
')'
unmatched → return
1
s = "(()"
→ one pair matched, one
'('
unmatched → return
1
s = ")()("
→ two matched in the middle, ends unmatched → return
2
s: string
int
0 <= len(s) <= 10^6
(design for linear time)