-
Notifications
You must be signed in to change notification settings - Fork 316
Expand file tree
/
Copy path32_LongestValidParentheses.py
More file actions
executable file
·39 lines (34 loc) · 1.12 KB
/
32_LongestValidParentheses.py
File metadata and controls
executable file
·39 lines (34 loc) · 1.12 KB
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
class Solution(object):
def longestValidParentheses(self, s):
"""
According to:
https://leetcode.com/discuss/7609/my-o-n-solution-using-a-stack
If current character is '(', push its index to the stack.
If current character is ')':
1. top of stack is '(', just find a matching pair so pop from the stack.
2. Otherwise, we push the index of ')' to the stack.
Finally the stack will only contain the indices of characters which cannot be matched.
Then the substring between adjacent indices should be valid parentheses.
"""
stack, longest = [0], 0
for i in xrange(1, len(s)):
if s[i] == '(':
stack.append(i)
else:
if stack and s[stack[-1]] == '(':
stack.pop()
valid_len = (i - stack[-1]) if stack else i + 1
longest = max(longest, valid_len)
else:
stack.append(i)
return longest
"""
""
")"
"()"
"))"
"(((()()()))("
"(((()()()))())"
"""