Valid Parentheses
Problem statement
Given a nonempty string s containing only (, ), [, ], {, and }, determine whether it is valid.
A string is valid when:
- Every opening bracket is closed by the same type of bracket.
- Open brackets are closed in the correct order.
- Every closing bracket has a corresponding opening bracket of the same type.
Return true when s is valid; otherwise, return false.
Function
isValidParentheses(s: String) → booleanExamples
Example 1
s = "()"return = trueThe opening parenthesis is closed by the matching closing parenthesis.
Example 2
s = "()[]{}"return = trueEach opening bracket closes with the same type, and no bracket closes out of order.
Example 3
s = "([)]"return = falseThe closing parenthesis appears before the nested square bracket is closed.
Example 4
s = "([])"return = trueThe square brackets close first, followed by the surrounding parentheses.
Constraints
1 <= s.length <= 10^4.- Every character in
sis one of()[]{}.