Skip to content

Instantly share code, notes, and snippets.

@deque-blog
Created February 27, 2020 14:07
Show Gist options
  • Save deque-blog/5ab6a894fd01a64bd5ae1435e8b1bb3b to your computer and use it in GitHub Desktop.
Save deque-blog/5ab6a894fd01a64bd5ae1435e8b1bb3b to your computer and use it in GitHub Desktop.
def longestValidParentheses(s: str) -> int:
def scan(s: str, opening_char: str) -> int:
longest = 0
opened = 0
start = 0
for i in range(len(s)):
if s[i] == opening_char:
opened += 1
elif opened > 0:
opened -= 1
if opened == 0:
longest = max(longest, i - start + 1)
else:
start = i + 1
return longest
return max(scan(s, '('), scan(s[::-1], ')'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment