Skip to content

Instantly share code, notes, and snippets.

@deque-blog
Last active February 27, 2020 13:33
Show Gist options
  • Save deque-blog/b72b8420b009c0bbd057fa08470d6406 to your computer and use it in GitHub Desktop.
Save deque-blog/b72b8420b009c0bbd057fa08470d6406 to your computer and use it in GitHub Desktop.
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