Valid Parentheses
Problem statement
Given a string s containing only ()[]{}, determine whether its brackets are balanced. Every closing bracket must match the most recent unmatched opening bracket of the same type, and every opening bracket must be closed.
For this exercise, assume the empty string is valid. Return a boolean.
Function
validParentheses(s: String) → booleanExamples
Example 1
s = "([]){}"return = trueEach pair has the correct type, and the square brackets close before their enclosing parentheses.
Example 2
s = "([)]"return = falseThe first closing parenthesis encounters an unmatched opening square bracket, so the nesting is invalid.
Constraints
- For this exercise, assume
0 <= s.length <= 10^5. - Every character is one of
( ) [ ] { }.