classSolution:defremoveInvalidParentheses(self, s:str)-> List[str]:
l = r =0for c in s:if c =='(':
l +=1elif c ==')':if l:
l -=1else:
r +=1
ans =[]@lru_cache(None)defdfs(idx, cl, cr, dl, dr, path):if idx ==len(s):ifnot dl andnot dr:
ans.append(path)returnif cr > cl or dl <0or dr <0:return
c = s[idx]if c =='(':
dfs(idx+1, cl, cr, dl-1, dr, path)elif c ==')':
dfs(idx+1, cl, cr, dl, dr-1, path)
dfs(idx+1, cl+(c=='('), cr+(c==')'), dl, dr, path+c)
dfs(0,0,0, l, r,"")return ans
删除无效的括号301.删除无效的括号题目描述思路:记忆化DFS301.删除无效的括号题目描述删除无效的括号思路:记忆化DFSclass Solution: def removeInvalidParentheses(self, s: str) -> List[str]: l = r = 0 for c in s: if c == '(': l += 1 elif c =