Count the Reversals- Data Structures and Algorithms
Given a string S consisting only of opening and closing curly brackets '{' and '}' find out the minimum number of reversals required to make a balanced expression
Input
The first line of input contains an integer T, denoting the number of test cases. Then T test cases
follow. The first line of each test case contains a string S consisting only of { and }.
Output
Print out minimum reversals required to make S balanced. If it cannot be balanced, then print -1
Examples
Input
4
}{{}}{{{
{{}}}}
{{}{{{}{{}}{{
{{{{}}}}
Output
3
1
-1
0
Solution:
For better understanding of this code,you should Check Parenthesis Checker
No comments:
Post a Comment