Part 7 of 17 in Stacks & Queues  

Check balanced parantheses in an expression by animeshn

Problem statement

Given an expression only containing upper-case alphabets: A-Z, operators: /, * , +, - and characters: (,),{,},[,] used to mark start and end of parentheses, validate whether the parentheses are balanced or not.

Input

First line of input will contain a number T = no. of test cases. Next T lines will each contain a string with alphanumeric characters or one of the characters '{', '}', '(','),'[' or ']'.

Output

For each test case print "YES" if the string has balanced parentheses, else print "NO"

Sample Input
7
()
)(
(2+2)
{3+(2+5)}
{}
(3*4+(6)
({2+5)}
Sample Output
YES
NO
YES
YES
YES
NO
NO


To try out your code



Sign in

Sign up