You are given a string ‘S’ that consists of lower case English letters and brackets.
Reverse the strings in each pair of matching parentheses, starting from the innermost one.
Your result should not contain any brackets.
Example :Suppose the given string ‘S’ is ‘((ng)ipm(ca))’ then the resultant reversed string on removing the parentheses here is ‘camping’ which can be done in the following steps:
-> ((ng)ipm(ca))
-> (gnipmac)
-> camping
The first line of input contains a single integer ‘T’ denoting the number of test cases given. Then next ‘T’ lines follow:
The first line of each test case input contains the length ‘N’ of the given string input ‘S’.
The second line of each test case input contains the given string input with matching parentheses ‘S’.
Output Format :
For every test case, return the final reversed string after reversing successive strings in each pair of matching parentheses, starting from the innermost one without any brackets.
Note :
You do not need to print anything; it has already been taken care of. Just Implement the given function.
Constraint :
1 <= ‘T’ <= 100
1 <= ‘N’ <= 1000
‘S’ contains only lowercase alphabets, ‘(‘ and ‘)’.
Where ‘T’ is the number of test cases given for the problem, ‘N’ is the length (‘S’.length) of the given string ‘S’. It's guaranteed that all parentheses are balanced.
Time Limit: 1 sec
2
10
(u(love)i)
18
(sajn(i(gn)ni)doc)
iloveu
codingninjas
Test Case 1 :
The output “iloveu” is the required reversed string ‘S’ after removing the given set of parentheses in the string. Here, the substring "love" is reversed first, then the whole string is reversed,
-> (u(love)i)
-> (uevoli)
-> iloveu
Test Case 2 :
The output “codingninjas” is the required string ‘S’ after removing the given set of parentheses in the string. Here, the substring "gn" is reversed first, then “ingni”, and finally, the whole string is reversed,
-> (sajn(i(gn)ni)doc)
-> (sajn(ingni)doc)
-> codingninjas
2
21
a(bcdefghijkl(mno)p)q
13
((ng)ipm(ca))
apmnolkjihgfedcbq
camping
Try to use strings to save different substrings in a pair of matched brackets and a stack to save those strings.
A possible solution to this problem could be to use a stack initialized by pushing an empty string. First, check whenever a ‘(‘ is encountered. Push another empty string in that case into the stack, and whenever a ‘)’ is encountered, then get the top element of the stack and reverse the string that is stored at the top of the stack. Append that reversed string to the remaining element that is now on top of the stack and update that with this. Else if the character is neither '(' nor ‘)’, update the top of the stack by appending the character to the string that is presently on top. Follow this for the rest of the string and finally pop the top of the stack and print the updated string.
The algorithm to calculate the required will be:-
O(N^2), where ‘N’ is the length of the given string ‘S’.
We are iterating over all the characters of the string ‘S’, and in every iteration (which is of order O(‘N’)) there is a string reversal taking place (which has a worst-case complexity of O(N)) hence, the complexity here grows by O(N^2).
O(N), where ‘N’ is the length of the given string ‘S’.
Since we are storing strings of lengths up to size ‘N’ in our stack that we are using, therefore, the space complexity is O(N).