nathanwailes

LeetCode 1249 - Minimum Remove to Make Valid Parentheses - 53ms solution

Mar 9th, 2024
922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.43 KB | None | 0 0
  1. class Solution:
  2.     def minRemoveToMakeValid(self, s: str) -> str:
  3.         stack = []
  4.         s = list(s)
  5.         for i, c in enumerate(s):
  6.             if c == '(':
  7.                 stack.append(i)
  8.             elif c == ')':
  9.                 if stack:
  10.                     stack.pop()
  11.                 else:
  12.                     s[i] = ''
  13.         while stack:
  14.             s[stack.pop()] = ''
  15.         return ''.join(s)
  16.        
Advertisement
Add Comment
Please, Sign In to add comment