Compute unmatched parentheses length
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
## Problem
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).
### Examples
- `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`
### Function signature
- Input: `s: string`
- Output: `int`
### Constraints
- `0 <= len(s) <= 10^6` (design for linear time)
### Notes
- You only need to return the number of unmatched parentheses, not the corrected string.
Quick Answer: This question evaluates string-processing skills and understanding of parentheses matching and cancellation, focusing on linear-time sequence analysis and edge-case handling.
Return the number of parentheses left after canceling all valid matches.
Constraints
- Inputs are Python literals matching the function signature.
- Return a deterministic exact-match value.
Examples
Input: ('()',)
Expected Output: 0
Explanation: All matched.
Input: ('())',)
Expected Output: 1
Explanation: One unmatched close.
Input: ('(()',)
Expected Output: 1
Explanation: One unmatched open.
Input: (')()(',)
Expected Output: 2
Explanation: Two unmatched ends.
Hints
- Clarify edge cases before coding.
- Keep the return value deterministic.