Search⌘ K
AI Features

Problem: Longest Valid Parentheses

Explore how to determine the longest valid parentheses substring in a string by using a stack to track indices of opening brackets. Learn to implement this efficiently in Python, understanding the step-by-step matching process and how to calculate substring lengths. This lesson helps you grasp essential string algorithm techniques with a linear time complexity solution.

Statement

Given a string s consisting solely of the characters '(' and ')', determine the length of the longest valid (well-formed) parentheses substring contained within s.

A valid parentheses substring is one in which every opening parenthesis '(' has a corresponding closing parenthesis ')', and the pairs are properly nested.

Constraints:

  • 00 \leq s.length 3×104\leq 3 \times 10^4 ...