Skip to content

Commit 6346502

Browse files
Create 1898-maximum-number-of-removable-characters.py
1 parent 6350714 commit 6346502

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def maximumRemovals(self, s: str, p: str, removable: list[int]) -> int:
3+
return self.binary_search(s, p, removable)
4+
5+
def is_subsequence(self, s: str, p: str, removable: list[int], k: int) -> bool:
6+
removed_set = set(removable[:k])
7+
8+
i, j = 0, 0
9+
while i < len(s) and j < len(p):
10+
if i in removed_set or s[i] != p[j]:
11+
i += 1
12+
else:
13+
i += 1
14+
j += 1
15+
return j == len(p)
16+
17+
def binary_search(self, s: str, p: str, removable: list[int]) -> int:
18+
left, right = 0, len(removable) + 1
19+
20+
while left < right:
21+
mid = (left + right) // 2
22+
if self.is_subsequence(s, p, removable, mid):
23+
left = mid + 1
24+
else:
25+
right = mid
26+
27+
return left - 1

0 commit comments

Comments
 (0)