Created
February 27, 2020 13:41
-
-
Save deque-blog/55f28a58caad2396511f99b24eccd345 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def longestValidParentheses(s: str) -> int: | |
def longest_from(i: int) -> int: | |
longest = 0 | |
opened = 0 | |
for j in range(i, len(s)): | |
if s[j] == '(': | |
opened += 1 | |
elif opened > 0: | |
opened -= 1 | |
if opened == 0: | |
longest = j - i + 1 | |
else: | |
return longest | |
return longest | |
max_len = 0 | |
for i in range(len(s)): | |
max_len = max(max_len, longest_from(i)) | |
return max_len |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment