Maximum Number of Removable Characters

Medium Topics Company Tags

You are given lowercase strings s and p, with p initially a subsequence of s, and an array removable of distinct indices in s.

Choose a number k and remove the characters at the first k indices in removable. These indices always refer to positions in the original string, before any removals. Return the largest k for which p is still a subsequence of the remaining string.

A subsequence preserves character order but does not need to use adjacent positions. You must remove a prefix of removable, rather than choosing arbitrary indices from it.

Example 1:

Input: s = "abaca"
p = "aa"
removable = [1,2,3,0]

Output: 3

Explanation: Removing original indices 1, 2, and 3 leaves "aa", so p is still a subsequence. Removing index 0 next leaves only one a. The maximum prefix length is 3.

Example 2:

Input: s = "banana"
p = "ban"
removable = [5,4,3,2]

Output: 3

Explanation: Removing indices 5, 4, and 3 leaves "ban". Removing index 2 as well leaves "ba", which cannot contain "ban". Return 3.

Example 3:

Input: s = "abc"
p = "abc"
removable = []

Output: 0

Explanation: There are no removable indices. The original string already contains p, and the only possible choice is k = 0.

Constraints:

  • 1 <= p.length <= s.length <= 100000
  • 0 <= removable.length <= s.length
  • 0 <= removable[i] < s.length
  • All indices in removable are distinct.
  • s and p contain only lowercase English letters.
  • p is a subsequence of the original s.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.