## DEV Community

Abhishek Chaudhary

Posted on

# Remove Invalid Parentheses

Given a string `s` that contains parentheses and letters, remove the minimum number of invalid parentheses to make the input string valid.

Return all the possible results. You may return the answer in any order.

Example 1:

Input: s = "()())()"
Output: ["(())()","()()()"]

Example 2:

Input: s = "(a)())()"
Output: ["(a())()","(a)()()"]

Example 3:

Input: s = ")("
Output: [""]

Constraints:

• `1 <= s.length <= 25`
• `s` consists of lowercase English letters and parentheses `'('` and `')'`.
• There will be at most `20` parentheses in `s`.

SOLUTION:

``````class Solution:
def isValid(self, s: str) -> bool:
stack = []
valid = True
for bracket in s:
if bracket == '(':
stack.append(bracket)
elif bracket == ')':
if len(stack) > 0:
if stack.pop() + bracket != '()':
valid = False
break
else:
valid = False
break
if len(stack) > 0:
valid = False
return valid

def removeInvalidParentheses(self, s: str) -> List[str]:
n = len(s)
paths = [("", 0, 0)]
btoi = { '(': 1, ')': -1 }
i = 0
mindels = n
op = set()
visited = set()
while i < len(paths):
curr, j, currctr = paths[i]
if j == n:
if self.isValid(curr):
if n - len(curr) < mindels:
mindels = n - len(curr)
op = { curr }
elif n - len(curr) == mindels: