Zepruv › Coding problems

Longest Valid Parentheses

Hard · String, Dynamic Programming, Stack

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

Example 1

Input: s = "(()"
Output: 2

The longest valid parentheses substring is "()".

Example 2

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

The longest valid parentheses substring is "()()".

Example 3

Input: s = ""
Output: 0

Constraints

Starter code (python)

class Solution(object):
    def longestValidParentheses(self, s):
        """
        :type s: str
        :rtype: int
        """
        

Related problems

Previous: Remove Element · Next: Search in Rotated Sorted Array · All problems

Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.