括号字符串的最长有效长度

#
#
# @param str string字符串 the string
# @return int整型
#
class Solution:
def maxVaildLength(self , str ):
# write code here
dp=[0] * len(str)
for i in range(1,len(str)):
if str[i]==")":
prev= i - dp[i-1]-1
if prev >= 0 and str[prev]== "(":
dp[i]=dp[i-1]+2+(dp[prev-1] if prev >=1 else 0)
return max(dp)

参考链接:
​​​ https://leetcode-cn.com/problems/longest-valid-parentheses/solution/zui-chang-you-xiao-gua-hao-by-leetcode-solution/​