#K95367. Balanced Brackets
Balanced Brackets
Balanced Brackets
In this problem, you are given one or more sequences of brackets. Each sequence consists solely of the characters '(', ')', '{', '}', '[' and ']'. Your task is to determine whether each sequence is balanced. A sequence is considered balanced if every opening bracket has a corresponding closing bracket in the correct order. Formally, a sequence is balanced if it satisfies the following conditions:
- Every opening bracket ({, [, () is closed by the corresponding closing bracket (}, ], )).
- The brackets are closed in the correct order.
For example, the sequence ([{}])
is balanced, while ([)]
is not. The empty sequence is considered balanced.
Below are some sample cases:
Sample Input:
5 () ([{}]) {[()]} ([)] ][
Sample Output:
YES YES YES NO NO
Your program should read from the standard input and write to the standard output.
inputFormat
The first line of the input contains a single integer (T) (where (1 \leq T \leq 10^3)) representing the number of test cases. Each of the following (T) lines contains a non-empty string that represents a sequence of brackets.
outputFormat
For each test case, output a single line containing (YES) if the bracket sequence is balanced or (NO) otherwise.## sample
5
()
([{}])
{[()]}
([)]
][
YES
YES
YES
NO
NO
</p>