Skip to content

Instantly share code, notes, and snippets.

@les-peters
Created August 16, 2022 21:03
Show Gist options
  • Save les-peters/ef849be173460b518f81bd1126d2b180 to your computer and use it in GitHub Desktop.
Save les-peters/ef849be173460b518f81bd1126d2b180 to your computer and use it in GitHub Desktop.
Longest paren substring
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