We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 46d5e5f commit 68a87cbCopy full SHA for 68a87cb
1 file changed
leetcode/minremoveparens/minremoveparens.py
@@ -5,22 +5,20 @@
5
class Solution:
6
def minRemoveToMakeValid(self, s: str) -> str:
7
open_idx = []
8
- close_idx = []
9
remove_idx = []
10
ct = 0
11
for idx, c in enumerate(s):
12
if c == "(":
13
open_idx.append(idx)
14
ct += 1
15
if c == ")":
16
- close_idx.append(idx)
17
if ct == 0:
18
remove_idx.append(idx)
19
else:
20
ct -= 1
21
22
- for _ in range(ct):
23
- remove_idx.append(open_idx.pop())
+ if ct > 0:
+ remove_idx.extend(open_idx[-ct:])
24
25
return "".join(c for idx, c in enumerate(s) if not idx in remove_idx)
26
0 commit comments