Skip to content

Practice Longest Valid Parentheses

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#32
LeetCode Problem

Longest Valid Parentheses

Problem Statement

Given a string containing just the characters '(' and ')', return the length of the longest valid (well-formed) parentheses substring.

Source: Longest Valid Parentheses on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "(()"
Output
2

# Example 2

Input
s = ")()())"
Output
4

# Example 3

Input
s = ""
Output
0

Constraints

0 <= s.length <= 3 * 10^4
s[i] is '(', or ')'.

Related LeetCode Problems

Practice more problems similar to Longest Valid Parentheses.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.