728x90
class Solution:
def minRemoveToMakeValid(self, s: str) -> str:
s = list(s)
stack = []
for i, char in enumerate(s):
if char == '(':
stack.append(i)
elif char == ')':
if stack:
stack.pop()
else:
s[i] = ''
while stack:
s[stack.pop()] = ''
return ''.join(s)
- Convert string to list, because String is an immutable data structure in Python and it's much easier and memory-efficient to deal with a list for this task.
- Iterate through list
- Keep track of indices with open parentheses in the stack. In other words, when we come across open parenthesis we add an index to the stack.
- When we come across close parenthesis we pop an element from the stack. If the stack is empty we replace current list element with an empty string
- After iteration, we replace all indices we have in the stack with empty strings, because we don't have close parentheses for them.
- Convert list to string and return
728x90
'Leetcode' 카테고리의 다른 글
856. Score of Parentheses (0) | 2020.08.07 |
---|---|
1190. Reverse Substrings Between Each Pair of Parentheses (0) | 2020.08.05 |
739. Daily Temperatures (0) | 2020.08.03 |
921. Minimum Add to Make Parentheses Valid (0) | 2020.07.22 |
20. Valid Parentheses (0) | 2020.07.21 |
댓글