Find Maximum Removals From Source String
medium · 39.3% accepted · 165 likes · top 19%
array · hash table · two pointers · string · dynamic programming
Description
You are given a string source of size n, a string pattern that is a subsequence of source, and a sorted integer array targetIndices that contains distinct numbers in the range [0, n - 1].
We define an operation as removing a character at an index idx from source such that:
- idx is an element of targetIndices.
- pattern remains a subsequence of source after removing the character.
Performing an operation does not change the indices of the other characters in source. For example, if you remove 'c' from "acb", the character at index 2 would still be 'b'.
Return the maximum number of operations that can be performed.
Solution