Longest valid Parentheses
Hard Accuracy: 37.86% Submissions: 6844 Points: 8

Given a string S consisting of opening and closing parenthesis '(' and ')'. Find length of the longest valid parenthesis substring.

Example 1:

Input: S = ((()
Output: 2
Explaination: The longest valid 
parenthesis substring is "()".

Example 2:

Input: S = )()())
Output: 4
Explaination: The longest valid 
parenthesis substring is "()()".

Your Task:
You do not need to read input or print anything. Your task is to complete the function maxLength() which takes string S as input parameter and returns the length of the maximum valid parenthesis substring.

Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)

1 ≤ |S| ≤ 105  

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Longest valid Parentheses

Output Window