Longest Valid Parentheses
Given a string s containing only the characters '(' and ')', return the length of the longest valid (well-formed) parentheses substring.
A valid parentheses substring is a contiguous substring where every opening parenthesis '(' is matched with a later closing parenthesis ')' in the correct order.
Example 1
Input
s = "(()"Output
2The longest valid parentheses substring is "()", which has length 2.
Example 2
Input
s = ")()())"Output
4The longest valid parentheses substring is "()()", which has length 4.
Constraints
- 0 <= s.length <= 3 * 10^4
- s[i] is either '(' or ')'