Skip to content

Instantly share code, notes, and snippets.

@bluepnume
Created December 31, 2013 01:07
Show Gist options
  • Select an option

  • Save bluepnume/8190835 to your computer and use it in GitHub Desktop.

Select an option

Save bluepnume/8190835 to your computer and use it in GitHub Desktop.
strings = [
'(()',
')()())',
'()())))((())((()())',
'()())))(())()((()()))(((()(())(()())'
]
def longestParen(string):
max_start = 0
max_length = 0
for i in range(len(string)):
count = 0
for j, char in enumerate(string[i:]):
count += (1 if char == '(' else -1)
if count < 0:
break
if not count:
if j > max_length:
max_start, max_length = i, j + 1
return string[max_start:max_start+max_length]
for string in strings:
print string, '->', longestParen(string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment