s is made only of the six characters (, ), [, ], { and }. Return true if the brackets are balanced and false if they are not.
Balanced means two things. Every opening bracket is closed by a bracket of its own kind, and the pairs nest properly: the bracket opened last is the one closed first.
Example 1
Input
s = "[({})]"
Output
true
Explanation
{} opens and closes inside (), which opens and closes inside []. Every pair sits completely within the pair around it.
Example 2
Input
s = "{[}]"
Output
false
Explanation
the } arrives while [ is still open, so the two pairs cross instead of nesting.
Example 3
Input
s = "(()"
Output
false
Explanation
the ) closes the second (, so the first ( is never closed.
Constraints:
1 <= s.length <= 10^5
Tab indents. Press Esc, then Tab to leave the editor.
Ctrl or ⌘ + Enter runs the tests.
Run your code to see every test here. Nothing is submitted or recorded.