-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path32. Longest Valid Parentheses.py
52 lines (47 loc) · 1.27 KB
/
32. Longest Valid Parentheses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Solution(object):
def longestValidParentheses(self, s):
"""
:type s: str
:rtype: int
"""
if not s:
return 0
stack = []
ans = []
for i in range(len(s)):
if stack and s[i] == ')':
ans.append(stack.pop())
ans.append(i)
if s[i] == '(':
stack.append(i)
ans.sort()
i = 0
n = len(ans)
res = 0
while i < n:
j = i
while j < n - 1 and ans[j + 1] - ans[j] == 1:
j += 1
res = max(res, j - i + 1)
i = j + 1
return res
class Solution(object):
def longestValidParentheses(self, s):
"""
:type s: str
:rtype: int
"""
if not s:
return 0
dp = [0] * (len(s))
ans = 0
for i in range(1, len(s)):
if s[i] == ')':
if s[i - 1] == '(':
dp[i] = dp[i - 2] + 2
elif s[i - 1] == ')' and i - dp[i - 1] - 1 >= 0 and s[
i - dp[i - 1] - 1] == '(':
dp[i] = dp[i - 1] + 2 + dp[i - dp[i - 1] - 2]
if dp[i] > ans:
ans = dp[i]
return ans