Created
October 16, 2017 21:03
-
-
Save cixuuz/911714e8d6b7ce71ee37a4fa754d9ea1 to your computer and use it in GitHub Desktop.
[301. Remove Invalid Parentheses] #leetcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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