Created
August 16, 2022 21:03
-
-
Save les-peters/ef849be173460b518f81bd1126d2b180 to your computer and use it in GitHub Desktop.
Longest paren substring
This file contains 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
question = """ | |
Given a string s consisting of various parenthesis ( and ), | |
find the length of the longest valid parenthesis substring. | |
Example: | |
> parensSubstring('(()(') | |
> 2 | |
> parensSubstring(')()(()))') | |
> 6 | |
""" | |
import re | |
def parensSubstring(string): | |
length = 0 | |
p = re.compile(r'\(\)') | |
m = p.search(string) | |
while m: | |
length += 2 | |
string = re.sub(r'\(\)', '', string, 1) | |
m = p.search(string) | |
return length | |
print(parensSubstring('(()(')) | |
print(parensSubstring(')()(()))')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment