Cheese with Holes (Emmental) - Developing
Cheese with Holes (Emmental) - Developing
Useful note with basic structure, but still has holes to fill.
Click the cheese icon to learn more

Validating Parentheses with Counting

Author: guiferviz

Created:

Last Modified:

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:

  1. Start with balance = 0.
  2. Iterate through the string:
    • For (, add 1.
    • For ), subtract 1.
  3. If balance ever drops below 0, return False (too many closing parentheses).
  4. Return True if balance is 0 at the end.

Try modifying the code below to test your own logic. Test cases are provided and automatically run to validate your solution.

Initializing...
Initializing...
Initializing...