Skip to content

Instantly share code, notes, and snippets.

@agmezr
Last active August 3, 2018 03:32
Show Gist options
  • Save agmezr/2c56484d33b8d527637cfcacedfa5d98 to your computer and use it in GitHub Desktop.
Save agmezr/2c56484d33b8d527637cfcacedfa5d98 to your computer and use it in GitHub Desktop.
Reverse the strings contained in each pair of matching parentheses.
"""
Reverse the strings contained in each pair of matching parentheses, starting from the innermost pair.
The results string should not contain any parentheses.
"""
def reverseParentheses(s):
stack = [""]
for letter in s:
if letter == "(":
stack.append("")
elif letter == ")":
word = stack.pop()
stack[-1]+= word[::-1]
else:
stack[-1]+= letter
return stack[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment