

For “()” you get a score of 1.
For “x y” you get a score of x + y where x and y are individual pairs of balanced parentheses.
For “(x)” you get a score twice of x (i.e), 2 * score of x.
Suppose given string 'STR' is “( ( ) )”.
So we return ‘2’ as input is of the form “(x)”, therefore total score = 2 * score of “()” = 2 * 1 = 2.
You are not required to print anything explicitly. It has already been taken care of. Just implement the function.
The first line of input contains a single integer ‘T’ denoting the number of test cases.
The first line of each test case contains an integer ‘N’ representing the length of the string.
The second line of each test case contains a string ‘STR’ containing the balanced parentheses.
For each test case, return the score of the string using the rules given.
1 <= T <= 50
1<= |STR| <= 1000
STR[I] = { ‘(‘, ‘)’ }
Where ‘T’ represents the number of test cases and ‘STR’ represents the given string.
Time Limit: 1 second
The idea here is to use the recursion. If we encounter any ‘(‘ bracket and then ‘)’ bracket, we simply increase the score. Else if we found another ‘(‘ bracket, we call the recursively the same function and multiply the output by ‘2’ as for the nested parenthesis like ‘( ( ) )’, the answer will be 2 * score. In this way, we proceed to get our final answer.
The idea here is to iterate over the string and for every ‘ith’ character, we check if the character is ‘(‘ or not. If it is found to be true, then we insert the character onto the stack. Else we calculate the score of the inner parentheses and insert double of the score calculated into the stack.
Algorithm: