Remove Invalid Parentheses(dfs,bfs)
Input:
"()())()"
Output:
["()()()", "(())()"]Input:
"(a)())()"
Output:
["(a)()()", "(a())()"]Input:
")("
Output:
[""]Last updated
Input:
"()())()"
Output:
["()()()", "(())()"]Input:
"(a)())()"
Output:
["(a)()()", "(a())()"]Input:
")("
Output:
[""]Last updated
class Solution:
def removeInvalidParentheses(self, s):
"""
:type s: str
:rtype: List[str]
"""
lc = rc = 0
for c in s:
if c == '(':
lc += 1
elif c == ')':
if lc > 0:
lc -= 1
else:
rc += 1
ret = set()
path = ''
self.dfs(s, lc, rc, ret, path, 0, 0)
return list(ret)
def dfs(self, s, lc, rc, ret, path, pos, open):
if lc < 0 or rc < 0 or open < 0:
return
if pos == len(s):
if lc == 0 and rc == 0 and open == 0:
ret.add(str(path))
return
if s[pos] == '(':
path+=s[pos]
self.dfs(s, lc, rc, ret, path, pos + 1, open + 1)#用(
path = path[:-1]
self.dfs(s, lc - 1, rc, ret, path, pos + 1, open)
if s[pos] == ')':
path += s[pos]
self.dfs(s, lc, rc, ret, path, pos + 1, open - 1)#用)
path = path[:-1]
self.dfs(s, lc, rc - 1, ret, path, pos + 1, open)
else:
path += s[pos]
self.dfs(s, lc, rc, ret, path, pos + 1, open)#字母加
path = path[:-1]class Solution:
def removeInvalidParentheses(self, s):
"""
:type s: str
:rtype: List[str]
"""
q = {s}
ret = set()
while True:
valid = filter(self.isValid, q)
if valid:
return valid
q = {s[:i] + s[i + 1:] for s in q for i in range(len(s))}
# def isValid(self, s):
# l = 0
# for c in s:
# if c == '(':
# l += 1
# elif c == ')':
#
# l -= 1
#
# if l < 0:
# return False
#
# return l == 0
def isValid(self,s):
s = filter('()'.count, s)
while '()' in s:
s = s.replace('()','')
return not s