Longest Valid Parentheses - Problem
The Challenge: You're given a string containing only parentheses characters '(' and ')'. Your task is to find the length of the longest contiguous substring that forms valid (well-formed) parentheses.

What makes parentheses valid?
โ€ข Every opening parenthesis '(' must have a corresponding closing parenthesis ')'
โ€ข They must be in the correct order (no closing before its matching opening)

Example: In the string "(()", the longest valid parentheses substring is "()" with length 2.

Goal: Return the length of the longest valid parentheses substring, not the substring itself.

Input & Output

example_1.py โ€” Basic Valid Parentheses
$ Input: s = "(()" # String with one valid pair
โ€บ Output: 2
๐Ÿ’ก Note: The longest valid parentheses substring is "()" which has length 2. The first '(' cannot be matched.
example_2.py โ€” Multiple Valid Pairs
$ Input: s = ")()())" # String with consecutive valid pairs
โ€บ Output: 4
๐Ÿ’ก Note: The longest valid parentheses substring is "()()" which has length 4. The first and last characters cannot form valid pairs.
example_3.py โ€” Empty String Edge Case
$ Input: s = "" # Empty string
โ€บ Output: 0
๐Ÿ’ก Note: Empty string contains no parentheses, so the longest valid parentheses substring has length 0.

Constraints

  • 0 โ‰ค s.length โ‰ค 3 ร— 104
  • s[i] is either '(' or ')'
  • String contains only parentheses characters

Visualization

Tap to expand
Longest Valid Parentheses - Visual SolutionExample: "(()())" - Let's trace through the stack solutionInput: ( ( ) ( ) )Index: 0 1 2 3 4 5Stack Simulation:Start: stack = [-1] (base for calculation)i=0 '(': push 0 โ†’ stack = [-1, 0]i=1 '(': push 1 โ†’ stack = [-1, 0, 1]i=2 ')': pop 1 โ†’ stack = [-1, 0] โ†’ length = 2-0 = 2 โœ“i=3 '(': push 3 โ†’ stack = [-1, 0, 3]i=4 ')': pop 3 โ†’ stack = [-1, 0] โ†’ length = 4-0 = 4 โœ“i=5 ')': pop 0 โ†’ stack = [-1] โ†’ length = 5-(-1) = 6 โœ“Maximum length found: 6 (entire string is valid!)๐ŸŽฏ Key Insight: Stack tracks unmatched positions, enabling O(n) length calculation
Understanding the Visualization
1
Identify the Pattern
Valid parentheses form nested or sequential patterns where every '(' has a matching ')' after it
2
Track with Stack
Use a stack to remember positions of unmatched '(' characters and calculate lengths when we find matches
3
Calculate Lengths
When we find a ')', match it with the most recent '(' and calculate the length of the valid sequence
4
Handle Edge Cases
Manage cases where ')' appears without matching '(' by updating our base position for calculations
Key Takeaway
๐ŸŽฏ Key Insight: The stack-based approach efficiently tracks unmatched parentheses positions, allowing us to calculate the length of valid sequences in O(n) time by leveraging the stack's top element as a reference point for distance calculations.
Asked in
Google 42 Amazon 38 Meta 29 Microsoft 25 Apple 18
142.4K Views
High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen