Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Longest Valid Parentheses (hard)
On this page

Problem Statement

Try it yourself

Problem Statement

You are given a string containing just the characters '(' and ')'. Your task is to find the length of the longest valid (well-formed) parentheses substring.

Example 1:

  • Input: "(())"
  • Expected Output: 4
  • Justification: The entire string is a valid parentheses substring.

Example 2:

  • Input: ")()())"
  • Expected Output: 4
  • Justification: The longest valid parentheses substring is "()()".

Example 3:

  • Input: "(()"
  • Expected Output: 2
  • Justification: The longest valid parentheses substring is "()".

Constraints:

  • 0 <= s.length <= 3 * 10<sup>4</sup>
  • s[i] is '(', or ')'.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page

Problem Statement

Try it yourself