isBalancedParentheses 🌶️🌶️
Write a method that takes one String parameter, s, made up only of the characters '(' and ')', and returns true if the parentheses are balanced (every close paren has a matching open paren before it, and every open paren is eventually closed), or false otherwise. isBalancedParentheses("(())") returns true. isBalancedParentheses(")(") returns false, even though it has the same number of each character as "()" because the order matters, not just the count.
Keep a running count: add 1 for every '(' and subtract 1 for every ')'. If the parentheses are truly balanced, this count should be exactly 0 at the very end. That is necessary but not sufficient to prove balance, though — what else could you check, to cover a case like ")(" (same count of each, but not balanced)?
Ending at 0 is not enough by itself, ")(" also ends at 0. The real test is that the count must never go NEGATIVE at any point along the way (a close paren appearing before its matching open paren). Check this inside the loop, not just at the end.