Created
February 14, 2020 01:13
-
-
Save y56/731daddf8a95fb0b7000245984c2a074 to your computer and use it in GitHub Desktop.
longestValidParentheses
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
substring is countinuous. | |
class Solution: | |
def longestValidParentheses(self, s: str) -> int: | |
numof_unpaired_left = 0 | |
numof_illegal_right = 0 | |
for ele in s: | |
if ele == '(': | |
numof_unpaired_left += 1 | |
else: # ele == ')' | |
if numof_unpaired_left == 0: | |
numof_illegal_right += 1 | |
else: | |
# it means numof_unpaired_left > 0 | |
# numof_unpaired_left never < 0 | |
numof_unpaired_left -= 1 | |
print(numof_unpaired_left) | |
print(numof_illegal_right) | |
return len(s) - numof_unpaired_left - numof_illegal_right |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment