- 题目描述
- 题目分析
- 其中 s.length < 25, 可以用枚举暴力来做
- BFS一层一层枚举,这一层出现正确结果break
-
1 from collections import deque 2 3 class Solution: 4 def removeInvalidParentheses(self, s: str) -> List[str]: 5 6 def check(s): 7 cnt = 0 8 for it in s: 9 if it == '(': 10 cnt += 1 11 elif it == ')': 12 cnt -= 1 13 if cnt < 0: 14 return False 15 return cnt == 0 16 17 18 level = set([s]) 19 while True: 20 ans = list(filter(check, level)) 21 if ans: break 22 23 states = set() 24 for s in level: 25 for i, char in enumerate(s): 26 if char in '()': 27 state = s[:i] + s[i+1:] 28 # print(state) 29 if state not in states: 30 states.add(state) 31 level = list(states) 32 33 return ans
抓住青春的尾巴。。。