-
Notifications
You must be signed in to change notification settings - Fork 0
LC 0032 [H] Longest Valid Parentheses
Code with Senpai edited this page Jan 28, 2022
·
1 revision
class Solution:
def longestValidParentheses(self, s: str) -> int:
stack = [-1]
longest = 0
for i in range(len(s)):
top = stack[-1]
if len(stack) > 1 and s[top] == '(' and s[i] == ')':
stack.pop()
newTop = stack[-1]
longest = max(i - newTop, longest)
else:
stack.append(i)
return longest
class Solution:
def longestValidParentheses(self, s: str) -> int:
stack = [-1]
r = 0
for i in range(len(s)):
if s[i] == '(':
stack.append(i)
else:
stack.pop()
if not stack:
stack.append(i)
else:
r = max(r, i - stack[-1])
return r
footer