DEV Community

Abhishek Chaudhary
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:
                        op.add(curr)
            if j < n and j - len(curr) <= mindels:
                nctr = currctr + btoi.get(s[j], 0)
                if nctr >= 0 and (curr + s[j], j + 1) not in visited:
                    visited.add((curr + s[j], j + 1))
                    paths.append((curr + s[j], j + 1, nctr))
                if s[j] == '(' or s[j] == ')' and (curr, j + 1) not in visited:
                    visited.add((curr, j + 1))
                    paths.append((curr, j + 1, currctr))
            i += 1
        return list(op)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)