Find the length of the longest contiguous substring of balanced parentheses.
Given a string consisting only of the characters '(' and ')', determine the length of the longest contiguous substring that forms a valid parentheses sequence.
A parentheses substring is valid if it can be interpreted as a correctly matched sequence of opening and closing brackets.
A single string s containing only '(' and ')' characters.
Return one integer: the length of the longest valid contiguous parentheses substring.
s contains only '(' and ')'0Example 1
Input
s = "(()"
Output
2
Explanation
The longest valid contiguous substring is "()", which has length 2.
Example 2
Input
s = ")()())"
Output
4
Explanation
The longest valid contiguous substring is "()()", which has length 4.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.