Leftover Brackets After Pairing Up a Parenthesis String
Stacks & QueuesAuto-graded · 22 test casesLast updated
Count the parentheses left over after forming the maximum number of valid "(" before ")" pairs, testing a greedy counter scan or stack over a string.
Leftover Brackets
You are given a string s made up only of the characters ( and ).
Form as many disjoint pairs as possible, where each pair consists of one ( and one ) that appears later in the string. Each character may belong to at most one pair.
Return the number of characters that end up in no pair.
Details
- The order matters. A
)can only be paired with a(that appears to its left. - An empty string has no leftover characters, so the answer is
0. - The answer is the same regardless of which maximum pairing you choose.
Function
Implement count_leftover(s), which takes the string s and returns an integer: the number of characters not belonging to any pair in a maximum pairing.
Constraints
0 <= len(s) <= 10^5scontains only the characters(and).
Examples
Example 1
Input: count_leftover('()()')
Output: 0
Example 2
Input: count_leftover('((()')
Output: 2
Example 3
Input: count_leftover(')()(')
Output: 2
Example 4
Input: count_leftover('(()(())')
Output: 1
Hints
Hint 1
Think about what you need to remember while scanning left to right: does it matter which '(' is still waiting, or only how many?
Hint 2
When you see a ')' and nothing is waiting, it can never be paired later, because only a '(' to its left could have matched it. Count it as leftover right away.
Hint 3
At the end, any '(' still waiting has no closer after it. The answer is the waiting count plus the number of ')' that arrived with nothing waiting. Comparing total counts of '(' and ')' alone fails on inputs like ')('.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Scan the string from left to right, keeping two counters:
waiting_open: the number of(seen so far that have not yet been paired.stray_close: the number of)that appeared when no(was waiting.
For each character:
(: incrementwaiting_open.)withwaiting_open > 0: pair it with a waiting(and decrementwaiting_open.)withwaiting_open == 0: incrementstray_close.
The answer is waiting_open + stray_close.
Why it works
A stack is unnecessary because all ( characters are interchangeable. Only the count of waiting ones matters, not which they are.
Pairing greedily is optimal. A ) seen when nothing is waiting has no ( to its left that is still free, so it can never be paired. A ) seen when something is waiting can be paired now. Delaying it gains nothing, since the ( it would use can absorb at most one ), and that is exactly the pair given up. So the greedy scan reaches the maximum number of pairs P, and the leftover count is len(s) - 2P, which equals waiting_open + stray_close.
The order matters: for ")(" the answer is 2, whereas comparing the total counts of each character would wrongly give 0.
Complexity
- Time: O(n), one pass with constant work per character.
- Space: O(1), two integer counters.
Reference solution
def count_leftover(s: str) -> int:
waiting_open = 0
stray_close = 0
for ch in s:
if ch == '(':
waiting_open += 1
elif waiting_open > 0:
waiting_open -= 1
else:
stray_close += 1
return waiting_open + stray_close