← Back to challenges

Longest Valid Parentheses

PythonHardalgorithmshigher_order_functionslogicstrings

Instructions

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

Examples

longest_valid_parentheses("(()") ➞ 2
# Longest valid parentheses substring is "()"

longest_valid_parentheses(")()())") ➞ 4
# Longest valid parentheses substring is "()()"

longest_valid_parentheses("()))))(()())(") ➞ 6

Notes

N/A

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