Longest valid Parentheses


Given a string A containing just the characters ’(‘ and ’)’.

Find the length of the longest valid (well-formed) parentheses substring.



Input Format:

The only argument given is string A.

Output Format:

Return the length of the longest valid (well-formed) parentheses substring.

Constraints:

1 <= length(A) <= 750000

For Example

Input 1:
    A = "(()"
Output 1:
    2
    Explanation 1:
        The longest valid parentheses substring is "()", which has length = 2.

Input 2:
    A = ")()())"
Output 2:
    4
    Explanation 2:
        The longest valid parentheses substring is "()()", which has length = 4.
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Longest valid Parentheses on Interview Code Editor
Sign Up
to access hints and editorial solutions for Longest valid Parentheses
Asked In:

Discussion


Loading...
Click here to start solving coding interview questions