본문 바로가기
Leetcode

1190. Reverse Substrings Between Each Pair of Parentheses

by YGSEO 2020. 8. 5.
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

'Leetcode' 카테고리의 다른 글

856. Score of Parentheses  (0) 2020.08.10
856. Score of Parentheses  (0) 2020.08.07
1249. Minimum Remove to Make Valid Parentheses  (0) 2020.08.04
739. Daily Temperatures  (0) 2020.08.03
921. Minimum Add to Make Parentheses Valid  (0) 2020.07.22

댓글