Leetcode

1190. Reverse Substrings Between Each Pair of Parentheses

YGSEO 2020. 8. 5. 01:30
728x90
class Solution:
    def reverseParentheses(self, s: str) -> str:
		#Take an empty stack for iteration
        stack = []
        
        for i in range(0, len(s)):
			# If the current charector is anything other than closing bracket append it
            
            if s[i] != ')':
                stack.append(s[i])
                
            else:
                k = []
				
                while len(stack)!=0 and stack[-1]!='(':
                    k.append(stack.pop()) # pop from stack and append: backward stack until opening bracket
				
                stack.pop() #Pop the ')' opening brace
                k = k[::-1] #reverse the order
                while len(k)!=0:
                    stack.append(k.pop()) #stack to original
		#return Stack
        return "".join(stack)

 

출처: leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/discuss/414098/Python-100-memory-with-Comments%3A-Easy-Understand

 

728x90