
The first line of input contains an integer ‘T' which denotes the number of test cases or queries to be run. Then the test cases follow.
The first and the only line of each test case contains a string ‘STR’.
For each test case, print ‘1’ if given ‘STR’ is complex else print ‘0’.
Print the output of each test case in a separate line.
1 <= ‘T’ <= 100
1 <= |STR| <= 5000
Where ‘T’ denotes the total number of test cases, |STR| represents the length of ‘STR’.
Time Limit: 1 second
The idea is to use the stack as using this, we can easily keep track of the expression between ‘(‘ and ‘)’ brackets. Basically, if we are able to find an expression surrounded by ‘(‘ and ‘)’, and if we are again left with ‘(‘ and ‘)’ as part of the ‘STR’, it implies there must be some unnecessary multiple brackets.
For example for ‘STR’ = “((a+b))”, after getting the expression “(a+b)” from the stack, we will be left with “()” which is an unnecessary bracket pair.
Here is the algorithm:
Create a stack of a type character. Iterate through ‘STR’ and for each character in the expression do the following:
Following two cases can arise while popping from the stack-