Problem · String
Valid Parentheses
Learn this problemProblem statement
Given a string s containing only parentheses, square brackets, and curly braces, return whether it is valid.
A string is valid when all of these rules hold:
- Every opening bracket is closed by the same bracket type.
- Opening brackets are closed in reverse order of how they were opened.
- Every closing bracket has a matching earlier opening bracket.
The empty string is valid.
Function
isValidParentheses(s: String) → booleanExamples
Example 1
s = "()[]{}"return = trueEach adjacent pair has the correct matching type.
Example 2
s = "([)]"return = falseThe square bracket opens after the parenthesis, so it must close before the parenthesis.
Example 3
s = "{[]}"return = trueThe brackets are correctly nested and every opener is closed.
Constraints
0 <= s.length <= 100000- Every character in
sis one of(,),[,],{, or}.