Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created October 16, 2017 21:03
Show Gist options
  • Save cixuuz/911714e8d6b7ce71ee37a4fa754d9ea1 to your computer and use it in GitHub Desktop.
Save cixuuz/911714e8d6b7ce71ee37a4fa754d9ea1 to your computer and use it in GitHub Desktop.
[301. Remove Invalid Parentheses] #leetcode
# BFS
class Solution(object):
def removeInvalidParentheses(self, s):
"""
:type s: str
:rtype: List[str]
"""
level = {s}
while True:
valid = filter(self.is_valid, level)
if valid:
return valid
level = {s[:i] + s[i+1:] for s in level for i in range(len(s))}
def is_valid(self, s):
count = 0
for c in s:
if c == "(":
count += 1
elif c == ")":
count -= 1
if count < 0:
return False
return count == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment