Created
August 9, 2023 13:20
-
-
Save kumrzz/bea60180dcc1bf2498c841256ae5fca4 to your computer and use it in GitHub Desktop.
leetcode1190 Reverse Substrings Between Each Pair of Parentheses, mostly from ravensmove.com/
This file contains hidden or 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
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