Embercode
✕
Stacks and QueuesCoding problems

Stacks and Queues: coding problems

Write a program that reads the input and prints the answer. Run checks it against every test, including hidden ones.

0 of 8 solved

  • A print shop checks every typeset line before it goes to press. Each line is a string of brackets. The shop only accepts a line when every opening bracket is closed by the matching kind, in the right order.

    Read n lines of brackets. For each line print YES if it is balanced, or NO if it is not. A stack is the natural tool: push each opener, and when a closer appears it must match the top.

    Constraints: 1 <= n <= 100. Each line has between 1 and 200 characters, and each character is one of ()[]{}. A line never contains spaces.

    Input. Line 1: `n`. The next `n` lines: one bracket string each.

    Output. `n` lines. Each line is `YES` or `NO`.

    Example 1 input

    3
    ({[]})
    ([)]
    ((
    

    Output

    YES
    NO
    NO
    

    Example 2 input

    2
    ()[]{}
    }
    

    Output

    YES
    NO