Hard · Dynamic Programming

Longest Valid Parentheses

Given a string s made only of '(' and ')', return the length of its longest contiguous substring that is a well-formed parentheses string, or 0 if there is none.

Examples

Example 1

s = "(()"

Output: length 2

Example 2

s = ")()())"

Output: length 4

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Dynamic Programming problems