


The length of the smallest valid substring '()' is 2.
'STR' = “()()())” here we can see that except the last parentheses all the brackets are making a valid parenthesis. Therefore, the answer for this will be 6.
The first line of input contains an integer 'T' denoting the number of test cases.
The first and the only line of each test case contains a string ‘str’ of which you have to tell the longest valid parentheses substring.
For each test case, return a single integer which is the length of the longest valid substring.
You don’t have to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= |STR| <= 3000
where |STR| is the length of the given string.
Time Limit: 1 sec
The idea is to check for all the substrings of the given string and then check for each substring whether it is valid or not.
The idea is to store indices of previous starting brackets in a stack. The first element of the stack is an element that provides an index before the beginning of the valid substring that is the base for the next valid substring.