Longest Valid Parentheses

Published by Mubashir Hassan in

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

Examples

longestValidParentheses("(()") ➞ 2
// Longest valid parentheses substring is "()"

longestValidParentheses(")()())") ➞ 4
// Longest valid parentheses substring is "()()"

longestValidParentheses("()))))(()())(") ➞ 6

Notes

N/A

Watch a quick demo on how Edabit works.