Skip to content

Latest commit

 

History

History
 
 

longestValidParentheses

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Problem

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

For "(()", the longest valid parentheses substring is "()", which has
length = 2.

Another example is ")()())", where the longest valid parentheses
substring is "()()", which has length = 4.

Solution

Maintain stack of last unmatched index in string