Created
February 27, 2020 14:07
-
-
Save deque-blog/5ab6a894fd01a64bd5ae1435e8b1bb3b 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 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