Loading...
Loading...
Loading...
A code editor must check that every bracket in a line is closed in the right order. The brackets are (), [] and {}. Print YES if the line is balanced and NO if it is not.
Example 1
Input
{[()]}Output
YES
Every bracket is closed in the right order.
Example 2
Input
([)]
Output
NO
The ] arrives while ( is the latest open bracket.
Try the problem first. Open one hint at a time.
Push every opening bracket on a stack.
A closing bracket must match the bracket on top; pop it. If the stack is empty or the top is a different type, the answer is NO.
After the last character, the answer is YES only if the stack is empty.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor