The given formula consists of English alphabets, digits, β(β and β)β.
The given formula is always valid.
The first line of input contains an integer 'T' representing the number of test cases. Then the test cases follow.
The only line of each test case contains a string representing the formula.
For each test case, return the string representing the count of all the elements is printed.
The output for each test case is in a separate line.
You are not required to print the expected output, it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= |S| <= 10^4
Time Limit: 1 sec
The count of each element in the formula will not exceed 2^31.
The main idea behind this approach is to use recursion and find the count of atoms between a β(β and a β)β. Whenever we encounter a β(β, we make a recursive call to the function, which after execution, gives an unordered map consisting of the count of atoms within the current formula. Then, we can use that map to calculate the count of atoms in a string containing that parenthesis.
Following is the algorithm for this approach :
The main idea behind this approach is to use STACK and solve the problem iteratively. We store the current value of the numeric counter the STACK whenever we encounter a β)β. The value of the numeric counter indicates the count of the current element.
We use two variables:
We start traversing the formula from right to left and do the following :
After the traversal, return the string formed by joining the sorted key and value pairs in the map.