301. Remove Invalid Parentheses

301. Remove Invalid Parentheses

class Solution:
    def removeInvalidParentheses(self, s: str) -> List[str]:
        def isvalid(s):
            ctr=0
            for c in s:
                if c=='(':
                    ctr+=1
                elif c==')':
                    ctr-=1
                    if ctr<0:
                        return False
            return ctr==0
        level={s}
        while True:
            valid=filter(isvalid,level)
            valid1=list(valid)
            if len(valid1)>0:return valid1
            level={s[:i]+s[i+1:] for s in level for i in range(len(s))}

注意这种bfs的写法

你可能感兴趣的:(leetcode)