#K64812. Balanced Brackets

    ID: 32059 Type: Default 1000ms 256MiB

Balanced Brackets

Balanced Brackets

Given a string consisting only of the six bracket characters ( ) { } [ ], determine if the brackets are balanced. A string is called balanced if every opening bracket has a corresponding closing bracket in the correct order. Formally, for every prefix of the string, the number of opening brackets is not less than the number of closing brackets for the corresponding type and overall the total number of opening and closing brackets are equal.

For example, the string \( ()[]{} \) is balanced, while \( ([)] \) is not. Your task is to process multiple test cases and output for each test case whether the provided string is balanced.

The solution requires reading input from standard input and printing the answer to standard output.

inputFormat

The input begins with an integer \( t \) indicating the number of test cases. Each of the following \( t \) lines contains a non-empty string consisting only of the characters ( ) { } [ ].

Example:

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

outputFormat

For each test case, output a single line containing \( \texttt{YES} \) if the string is balanced, or \( \texttt{NO} \) otherwise.

Example:

YES
YES
NO
## sample
3
()[]{}
([{}])
([)]
YES

YES NO

</p>