Longest Valid Parentheses (Leetcode 32)
Problem Link: https://leetcode.com/problems/longest-valid-parentheses/
class Solution:
def longestValidParentheses(self, s: str) -> int:
ans=0
o=0
c=0
for i in s:
if(i=='('):
o+=1
else:
c+=1
if(c>o):
c=0
o=0
elif(o==c):
ans=max(ans,o+c)
o=0
c=0
for i in s[::-1]:
if(i==')'):
c+=1
else:
o+=1
if(o>c):
c=0
o=0
elif(o==c):
ans=max(ans,o+c)
return ans
PreviousFind All Numbers Disappeared in an Array (Leetcode 448)NextMinimum Time Difference (Leetcode 539)
Last updated