Balance Checker
Given a string containing just the characters '(' and ')', determine if the input string is valid. A string is considered valid if all opening parentheses have a corresponding closing parentheses in the correct order.
[ "()" ]
Explanation. The string contains one pair of matching parentheses.
[ "(())" ]
Explanation. The string contains two nested pairs of matching parentheses.
[ "(()" ]
Explanation. The string contains an unmatched opening parenthese.
[ ")(" ]
Explanation. The parentheses are in the wrong order.
[ "((()" ]
Explanation. There is one unmatched opening parenthese.
[ "()()" ]
Explanation. The string contains two non-nested pairs of matching parentheses.
Follow-up: How would you extend this problem to handle other types of brackets such as '{}' and '[]'?
The input string will only contain the characters '(' and ')'.
- Views
- 2