Question:
Length of the longest valid substring
A string consist of opening and closing parenthesis, so we need find the length of the longest valid parenthesis substring.
For Example:
str = ((()()()()
def findMaxLen(string):
n = len(string)
stack = []
stack.append(-1)
result = 0
for i in range(n):
if string[i] == '(':
stack.append(i)
else:
if len(stack) != 0:
stack.pop()
if len(stack) != 0:
result = max(result,
i - stack[len(stack) - 1])
else:
stack.append(i)
return result
string = "((()()()()"
print(findMaxLen(string))
string = "()(()))))())))"
print(findMaxLen(string))