Fundamentals/Valid Parentheses
← PrevNext →
For this question we ask you to determine whether or not a string has valid parentheses. A string has valid parentheses if each bracket is closed and opened in the same order and has the same type. Parentheses has 3 types: (), {} and [].
Input
s: String containing the parentheses
Output
Whether or not the string is valid.
Examples
Example 1
Input`s = "()"`
Output`true`
ExplanationOne pair, closed in order.
Example 2
Input`s = "()[]{}"`
Output`true`
ExplanationThree independent pairs, all closed in order.
Example 3
Input`s = "(]"`
Output`false`
Explanation`(` is closed by `]` — wrong type.
Example 4
Input`s = "([)]"`
Output`false`
ExplanationBrackets cross — `(` opened first must close last.
Example 5
Input`s = "{[]}"`
Output`true`
Explanation`[]` nested cleanly inside `{}`.
Constraints
1 <= s.length <= 100000
s consists only of the characters (, ), [, ], {, }.