A warehouse inventory tool encodes each container's nesting layout as a string made up only of the six bracket characters (, ), [, ], {, }. The layout is balanced when every opening bracket has a matching closing bracket of the same kind and the pairs are properly nested: the most recently opened bracket must always be the next one closed. Brackets of different kinds may never match each other (for example ( can only be closed by )).
Determine whether the given string is balanced.
A single line: the bracket string. It contains only the characters ()[]{} and no whitespace.
Print YES if the string is balanced, otherwise print NO.
(, ), [, ], {, }.Example 1
Input
([]{})Expected
YES
Explanation
Reading left to right: ( opens, [ opens then ] closes it, { opens then } closes it, and finally ) closes the (. Every closer matches the most recent opener of the same kind, so the string is balanced.
Example 2
Input
([)]
Expected
NO
Explanation
After ( and [ are open, the next character ) tries to close (, but the most recently opened bracket is [. The kinds interleave rather than nest, so it is not balanced.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →