Given a string s containing only '(' and ')', return the length of the longest valid (well-formed) parentheses substring. Example: Input: s = ")()())" Output: 4 Explanation: The longest valid substring is "()()" with length 4. Pattern focus: Monotonic Stack for matching indices.
s = parentheses string
length of the longest valid parentheses substring
Example 1:
Input:
s = "(()"
Output:
2
Explanation:
The substring '()' is the longest valid one.
Example 2:
Input:
s = ")()())"
Output:
4
Explanation:
The longest valid block is '()()'.