I am solving the following Leetcode problem: https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/
Here is my coded solution:
def minRemoveToMakeValid(s: str) -> str:
count = 0
pointer = 0
while pointer < len(s): # points to the last char of s
if count >= 0:
if s[pointer] == "(":
count += 1
elif s[pointer] == ")":
count -= 1
pointer += 1
print(pointer)
else:
if s[pointer] == ")":
s = s.replace(s[pointer], " ", 1)
count += 1
pointer +=1
if count > 0:
s = s[::-1]
s.replace("(", "", count)
s = s[::-1]
s = s.replace(" ", "")
return s
print(minRemoveToMakeValid("a)b(c)d"))
For some reason the while loop is stuck on 2
(as suggested by the print statement
)- any ideas why this is? I suspect it is because I am replacing ")"
, but in my mind I've accounted for this.