Understandable Python Solution

  • 1
    class Solution(object):
        def removeInvalidParentheses(self, s):
            :type s: str
            :rtype: List[str]
            # this checks if a string is invalid
            def isvalid(s):
                s = ''.join(list(filter('()'.count, s))) # eliminate all non-brackets
                while '()' in s:
                    s = s.replace('()', '')
                return not s
            store = {s}
            while True:
                valid = filter(isvalid, store)
                if valid:
                    return valid
                store = {s[:i] + s[i+1:] for s in store for i in range(len(s))} # add all possible brackets into store

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.