Back to DSA

Longest Valid Parentheses

hard
Acceptance: 36%
StringsTwo Pointers

A 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:2
Explanation: The longest well-matched segment is '()' with length 2.
Example 2:
Input:s = "(())()"
Output:6
Explanation: The entire string is validly matched, so the answer is 6.

Hints

00:00
1234567