-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path32.py
More file actions
24 lines (20 loc) · 955 Bytes
/
32.py
File metadata and controls
24 lines (20 loc) · 955 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# https://leetcode.com/problems/longest-valid-parentheses/description/
# 32. Longest Valid Parentheses
# reference: https://leetcode.com/problems/longest-valid-parentheses/solutions/5373015/stack-solution-video-explanation/
# using stack to store the index of waiting '('
class Solution:
def longestValidParentheses(self, s: str) -> int:
# using stack to store the index of waiting '('
# fill the value -1 to avoid confusion with index values (values >= 0).
stack = [-1]
maxLen = 0
for i in range(len(s)):
if s[i]=='(':
stack.append(i)
else: # s[i] == ')'
stack.pop()
if len(stack)==0: # previous recorded one is ')', not matched
stack.append(i)
else: # '(' and ')' matched, update maxLen if the new len is bigger
maxLen = max(maxLen, i-stack[-1])
return maxLen