1249. Minimum Remove to Make Valid Parentheses
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 71% Topics: String, Stack
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(n)
class Solution(object):
def minRemoveToMakeValid(self, s):
"""
:type s: str
:rtype: str
"""
result = list(s)
count = 0
for i, v in enumerate(result):
if v == '(':
count += 1
elif v == ')':
if count:
count -= 1
else:
result[i] = ""
if count:
for i in reversed(xrange(len(result))):
if result[i] == '(':
result[i] = ""
count -= 1
if not count:
break
return "".join(result)
Solution from kamyu104/LeetCode-Solutions · MIT