Skip to main content
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

Tags

Grind.
Last modified on August 25, 2026