Problem · String
Valid Parentheses
Learn this problemProblem statement
Given a string s containing only the bracket characters (, ), [, ], {, and }, return whether it is valid.
A bracket string is valid when both conditions hold:
- Every opening bracket is closed by the same type of bracket.
- Brackets close in the reverse order in which they were opened.
Function
isValidParentheses(s: String) → booleanExamples
Example 1
s = "()[]{}"return = trueEach opening bracket is immediately closed by the matching type, so the string is valid.
Example 2
s = "(]"return = falseThe opening parenthesis cannot be closed by ].
Example 3
s = "([{}])"return = trueThe bracket types are properly nested and close from the innermost pair outward.
Constraints
1 <= s.length <= 10^5scontains only()[]{}.