forked from y-ncao/Python-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongest_Valid_Parentheses.py
31 lines (28 loc) · 991 Bytes
/
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
"""
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
For "(()", the longest valid parentheses substring is "()", which has length = 2.
Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.
"""
class Solution:
# @param s, a string
# @return an integer
def longestValidParentheses(self, s):
N = len(s)
if N <= 1:
return 0
ret = 0
stack = []
last = 0
for i, char in enumerate(s):
if char == '(':
stack.append(i)
else:
if len(stack) == 0:
last = i + 1
else:
index = stack.pop()
if len(stack) == 0:
ret = max(ret, i - last + 1)
else:
ret = max(ret, i - stack[-1])
return ret