Longest valid Parentheses

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.

Interview Code Editor
Hints
  • Solution Approach
  • Complete Solution
1514 successful submissions.
Asked In:
  • Google
Click here to jump start your coding interview preparation