We have a string consisting only of ( and ) characters, and we need the minimum number of insertions that makes every parenthesis properly matched. We only insert characters, never remove them, so each parenthesis already in the string stays. The answer is the count of parentheses that are currently unmatched, because each one requires exactly one insertion to fix.
Every unmatched ( needs a ) inserted somewhere after it. Every unmatched ) needs a ( inserted somewhere before it. So the answer is the total count of unmatched parentheses.
There are two ways a parenthesis goes unmatched. Scanning left to right, a ) is unmatched when no open ( is waiting for it. A ( is unmatched when it reaches the end of the string without a ) pairing with it. Counting both types and summing them gives the answer.
1 <= s.length <= 1000: The string is short, so the linear solution below runs in well under a millisecond. The same approach scales to far longer inputs without change.s[i] is either '(' or ')'. There are no letters or other characters, so every character is one of the two parentheses.A stack tracks which opening parentheses are still waiting for a match. Push every (. On each ), try to match it with a ( from the top of the stack. If the stack is not empty, pop it (a matched pair). If the stack is empty, this ) has no ( before it to pair with, so it is unmatched and needs a ( inserted for it.
After scanning the entire string, any ( characters still on the stack are unmatched and each needs a ) inserted. So the answer is: unmatched ) we found during the scan + unmatched ( left on the stack.
unmatchedClose set to 0.(, push it onto the stack.):unmatchedClose (this ) has no match).(.stack.size() + unmatchedClose.The stack approach uses O(n) extra space, yet the only thing it reads from the stack is its size, never the elements. The next approach replaces the stack with a single counter and drops the space to O(1).
Since the string only contains ( and ), a full stack is unnecessary. Every element pushed in Approach 1 is the same character (, so the stack carries no information beyond its height. A counter unmatchedOpen holds that height directly: the number of unmatched ( seen so far.
On (, increment unmatchedOpen. On ), decrement unmatchedOpen if it is positive (an unmatched ( is available to pair with), otherwise increment a separate counter unmatchedClose (no ( is available, so this ) is unmatched). The answer is unmatchedOpen + unmatchedClose.
Collapsing the stack to a counter is valid only because there is a single type of opening bracket. With (, [, and {, a ) must match the specific bracket type on top of the stack, so the order and identity of stored brackets matter, and a count alone cannot tell which type is on top. With only ( and ), every stored bracket is identical, so the height carries all the information a ) needs to match. That makes unmatchedOpen a faithful stand-in for the stack size at every step.
unmatchedOpen = 0 and unmatchedClose = 0.(, increment unmatchedOpen.):unmatchedOpen > 0, decrement unmatchedOpen (matched a pair).unmatchedClose (no ( available to match).unmatchedOpen + unmatchedClose.