Problem statement: Given a string
s consisting of open and closed brackets
")", return the length of the longest substring in
s that is a valid string of parentheses.
s = ")(())(()"
"(())" is the longest substring with valid parentheses.
Naive approach: Find all the substrings of a given string. For every string, check…