Problem title
Difficulty
Avg time to solve

Wildcard Queries
Hard
50 mins
Happy Residents
Moderate
15 mins
K-Palindrome
Hard
--
Stack Implementation Using Array
Easy
20 mins
Path In A Tree
Moderate
--
Sort Odd-Even Ways
Moderate
--
Ninja And The LCM
Easy
15 mins
Properties of MST in a Undirected Graph
Easy
15 mins
Sum Tree
Moderate
29 mins
Number Pattern
Easy
--
5

# Longest Valid Parentheses

Difficulty: MEDIUM
Contributed By
Priyaraj |Level 1
Avg. time to solve
10 min
Success Rate
90%

Problem Statement

#### You are given a string ‘S’ containing only the characters ‘)’ and ‘(‘. You need to find the length of the longest valid i.e. well-formed parentheses substring.

##### For example:
``````Let the given string be “(()())((”.

Here the valid parentheses substrings are: “()”, “()” and “(()())”. Out of these the longest valid string is “(()())” which has a length 6.
``````
##### Input Format:
``````The first line of input contains an integer ‘T’ representing the number of test cases.

The first and the only line of every test case contains the string S.
``````
##### Output Format:
``````For each test case, the length of the longest valid (well-formed) parentheses substring is printed.

The output for each test case is printed in a separate line.
``````

#### Note:

``````You don’t need to print anything. It has already been taken care of. Just implement the given function.
``````
##### Constraints:
``````1 <= T <= 100
1 <= Length(S) <= 10^4

Where ‘T’ is the number of test cases and ‘S’ is the given string.

Time Limit: 1sec
``````
##### Sample Input 1:
``````2
)()()))
)))(((
``````
##### Sample Output 1:
``````4
0
``````
##### Explanation For Sample Input 1:
``````For the first test case, the longest valid (well-formed) parentheses substring is “()()” with length 4.

For the second test case, there is no valid parentheses substring. Hence, the output is 0.
``````
##### Sample Input 2:
``````2
()((())()
()()()()()()()
``````
##### Sample Output 2:
``````6
14
``````
Console