Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kumrzz/bea60180dcc1bf2498c841256ae5fca4 to your computer and use it in GitHub Desktop.
Save kumrzz/bea60180dcc1bf2498c841256ae5fca4 to your computer and use it in GitHub Desktop.
leetcode1190 Reverse Substrings Between Each Pair of Parentheses, mostly from ravensmove.com/
class Solution(object):
def reverseParentheses(s):
if not s:
return ''
arr = []
for char in s:
if char == ')':
combine_str = ''
while arr and arr[-1] != '(':
elem = arr.pop()[::-1]
combine_str += elem
arr.pop()
arr.append(combine_str)
else:
arr.append(char)
return "".join(arr)
# answer 1
s = "(abcd)"
print(Solution.reverseParentheses(s))
# answer 2
s = "(u(love)i)"
print(Solution.reverseParentheses(s))
# answer 3
s = "(ed(et(oc))el)"
print(Solution.reverseParentheses(s))
# answer 4
s = "a(bcdefghijkl(mno)p)q"
print(Solution.reverseParentheses(s))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment