classSolution:# @param s, a string# @return an integerdeflongestValidParentheses(self,s):ifs==''ors=='('ors==')':return0stack=[(-1,')')]maxLen=0foriinxrange(len(s)):ifs[i]==')'andstack[-1][1]=='(':stack.pop()maxLen=max(maxLen,i-stack[-1][0])else:stack.append((i,s[i]))returnmaxLen#tests=Solution()prints.longestValidParentheses("()(()")prints.longestValidParentheses('(()()')prints.longestValidParentheses(')()())')