Given a string containing just `(` and `)`, return the length of the longest valid (well-formed) parentheses substring.
**Example 1:**
```
Input: s = "(()"
Output: 4
Explanation: The longest valid parentheses substring is "()".
```
**Example 2:**
```
Input: s = ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()".
```
**Example 3:**
```
Input: s = ""
Output: 0
```
Expected Time Complexity
O(n)
Expected Space Complexity
O(n)
Example Test Cases
Example 1
Input:
")()())"
Output:
4
Example 2
Input:
"(()"
Output:
4
Previous Submission
You previously submitted a solution on 3/26/2026, 2:48:38 PM. Status: ACCEPTED