Finish problem 1898-maximum-number-of-removable-characters

This commit is contained in:
Johan Sandoval
2026-07-25 15:32:54 -05:00
parent 7d56ecae01
commit b82b09aa8b
2 changed files with 36 additions and 0 deletions
@@ -0,0 +1,22 @@
def p_still_in_s(s: str, p: str, removable: list[int], chars_removed: int) -> bool:
removed = set(removable[:chars_removed])
p_index = 0
for i, char in enumerate(s):
if i in removed:
continue
if p_index < len(p) and char == p[p_index]:
p_index += 1
return p_index == len(p)
class Solution:
def maximumRemovals(self, s: str, p: str, removable: list[int]) -> int:
lo, hi = 0, len(removable)
while lo < hi:
chars_removed = (lo + hi + 1) // 2
if p_still_in_s(s, p, removable, chars_removed):
lo = chars_removed
else:
hi = chars_removed - 1
return lo
@@ -0,0 +1,14 @@
import pytest
from solution import Solution
@pytest.mark.parametrize(
"s, p, removable, expected",
[
("abcacb", "ab", [3, 1, 0], 2),
("abcbddddd", "abcd", [3, 2, 1, 4, 5, 6], 1),
("abcab", "abc", [0, 1, 2, 3, 4], 0),
],
)
def test_maximum_removals(s, p, removable, expected):
assert Solution().maximumRemovals(s, p, removable) == expected