LeetCode 32, Hard. Topics: String, Dynamic Programming, Stack. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 16 parametrized pytest cases, and a playground notebook:
Problem
Given a string containing just the characters '(' and ')', return the length of the longest valid (well-formed) parentheses substring.
Examples
Constraints
- 0 <= s.length <= 3 * 10^4
s[i] is '(', or ')'.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Grind. Last modified on September 7, 2026