Back to DSA
Longest Valid Parentheses
hardA string consisting exclusively of opening '(' and closing ')' parentheses is given. Determine the length of the longest contiguous substring that forms a correctly matched sequence of parentheses.
Examples
Example 1:
Input:
s = "()(()"Output:
2Explanation: The longest well-matched segment is '()' with length 2.
Example 2:
Input:
s = "(())()"Output:
6Explanation: The entire string is validly matched, so the answer is 6.
Hints
1234567