Skip to content

Instantly share code, notes, and snippets.

@y56
Created February 14, 2020 01:13
Show Gist options
  • Save y56/731daddf8a95fb0b7000245984c2a074 to your computer and use it in GitHub Desktop.
Save y56/731daddf8a95fb0b7000245984c2a074 to your computer and use it in GitHub Desktop.
longestValidParentheses
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