> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum Number of Removable Characters

> Tested Python solution for LeetCode 1898 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 1898, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [String](/catalog/topics/string), [Binary Search](/catalog/topics/binary-search). [View on LeetCode](https://leetcode.com/problems/maximum-number-of-removable-characters/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1898   # by problem number
lcpy gen -s maximum_number_of_removable_characters   # by problem name
```

## Problem

You are given two strings `s` and `p` where `p` is a subsequence of `s`. You are also given a **distinct 0-indexed** integer array `removable` containing a subset of indices of `s` (`s` is also **0-indexed**).

You want to choose an integer `k` (`0 <= k <= removable.length`) such that, after removing `k` characters from `s` using the **first** `k` indices in `removable`, `p` is still a subsequence of `s`. More formally, you will mark the character at `s[removable[i]]` for each `0 <= i < k`, then remove all marked characters and check if `p` is still a subsequence.

Return *the **maximum** `k` you can choose such that `p` is still a subsequence of `s` after the removals*.

A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.

### Examples

```
Input: s = "abcacb", p = "ab", removable = [3,1,0]
Output: 2
```

**Explanation:** After removing the characters at indices 3 and 1, `"abcacb"` becomes `"accb"`. `"ab"` is a subsequence of `"accb"`. If we remove the characters at indices 3, 1, and 0, `"abcacb"` becomes `"ccb"`, and `"ab"` is no longer a subsequence. Hence, the maximum k is 2.

```
Input: s = "abcbddddd", p = "abcd", removable = [3,2,1,4,5,6]
Output: 1
```

**Explanation:** After removing the character at index 3, `"abcbddddd"` becomes `"abcddddd"`. `"abcd"` is a subsequence of `"abcddddd"`.

```
Input: s = "abcab", p = "abc", removable = [0,1,2,3,4]
Output: 0
```

**Explanation:** If you remove the first index in the array removable, `"abc"` is no longer a subsequence.

### Constraints

* `1 <= p.length <= s.length <= 10^5`
* `0 <= removable.length < s.length`
* `0 <= removable[i] < s.length`
* `p` is a subsequence of `s`.
* `s` and `p` both consist of lowercase English letters.
* The elements in `removable` are distinct.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_number_of_removable_characters/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_number_of_removable_characters/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(len(s) * log(len(removable)))
    # Space: O(len(removable))
    def maximum_removals(self, s: str, p: str, removable: list[int]) -> int:
        def is_subsequence(removed: set[int]) -> bool:
            i = 0
            for j, ch in enumerate(s):
                if i == len(p):
                    return True
                if j in removed or ch != p[i]:
                    continue
                i += 1
            return i == len(p)

        lo, hi = 0, len(removable)
        while lo < hi:
            mid = (lo + hi + 1) // 2
            if is_subsequence(set(removable[:mid])):
                lo = mid
            else:
                hi = mid - 1
        return lo
```

## Complexity

| Time | Space |
| - | - |
| O(len(s) \* log(len(removable))) | O(len(removable)) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.