← Back to challenges

Longest Valid Parentheses

JavaScriptHardalgorithmshigher_order_functionslogicstrings

Instructions

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

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.