Skip to content

Instantly share code, notes, and snippets.

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