-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveInvalidParentheses.py
More file actions
44 lines (40 loc) · 1.24 KB
/
removeInvalidParentheses.py
File metadata and controls
44 lines (40 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/remove-invalid-parentheses/
# Author: Miao Zhang
# Date: 2021-02-01
class Solution:
def removeInvalidParentheses(self, s: str) -> List[str]:
l = 0
r = 0
for c in s:
if c == '(': l += 1
if c == ')':
if l == 0:
r += 1
else:
l -= 1
res = []
self.dfs(s, 0, l, r, res)
return res
def isValid(self, s: str) -> bool:
cnt = 0
for c in s:
if c == '(':
cnt += 1
if c == ')':
cnt -= 1
if cnt < 0:
return False
return cnt == 0
def dfs(self, s: str, start: int, l: int, r: int, res: List[str]) -> None:
if l == 0 and r == 0:
if self.isValid(s):
res.append(s)
return
for i in range(start, len(s)):
if i != start and s[i] == s[i - 1]: continue
if r > 0 and s[i] == ')':
self.dfs(s[:i] + s[i + 1:], i, l, r - 1, res)
if l > 0 and s[i] == '(':
self.dfs(s[:i] + s[i + 1:], i, l - 1, r, res)