Given an expression formed only with standard parentheses ( and ), return
whether they are balanced.
This is a simplified version of the classic
Valid Parentheses problem.
Because we only have one type of parenthesis, we can solve this by simply
counting balance.
The Logic:
- Start with
balance = 0. - Iterate through the string:
- For
(, add 1. - For
), subtract 1.
- For
- If
balanceever drops below 0, returnFalse(too many closing parentheses). - Return
Trueifbalanceis 0 at the end.
Try modifying the code below to test your own logic. Test cases are provided to validate your implementation.
Bytes