Last active
February 27, 2020 13:33
-
-
Save deque-blog/b72b8420b009c0bbd057fa08470d6406 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 is_valid(i: int, j: int) -> bool: | |
opened = 0 | |
for c in s[i:j+1]: | |
if c == '(': | |
opened += 1 | |
elif opened > 0: | |
opened -= 1 | |
else: | |
return False | |
return opened == 0 | |
max_len = 0 | |
for i in range(len(s)): | |
for j in range(len(s)): | |
if is_valid(i, j): | |
max_len = max(max_len, j - i + 1) | |
return max_len |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment