Valid Parentheses
The string s holds only the characters (, ), [, ], { and }. Return true if it is balanced:
- every opening bracket is closed by a bracket of the same kind,
- brackets close in the opposite order to how they opened (
([)]is not balanced), - every closing bracket has an opening one before it.
Constraints
- 1 ≤ length of
s≤ 10,000
Example 1
Input
s = "()[]{}"
Output
true
Why:
Three pairs, each closed by its own kind.
Example 2
Input
s = "([)]"
Output
false
Why:
[ opened after (, so it must close before ).
Example 3
Input
s = "{[]}"
Output
true
Why:
[] sits inside {}.
Hints
Was this a good challenge?
- Solved by
- 0
- Submissions
- 0
- Accepted
- –