Last active
August 3, 2018 03:32
-
-
Save agmezr/2c56484d33b8d527637cfcacedfa5d98 to your computer and use it in GitHub Desktop.
Reverse the strings contained in each pair of matching parentheses.
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
""" | |
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