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

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

Watch a quick demo on how Edabit works.