Problem Description
Given a string source, a subsequence string pattern, and a sorted array targetIndices representing indices in source that can be removed, return the maximum number of removal operations that can be performed such that pattern remains a subsequence of source. During each removal the indices of remaining characters remain unchanged.
Key Insights
- Use binary search to determine the maximum count of removable indices.
- For each candidate number of removals, simulate removals by marking the characters at the specified indices.
- Verify if pattern remains a subsequence of the modified source using a two-pointers approach.
- Since removals do not shift the indices, a boolean array can easily represent which characters are removed.
Space and Time Complexity
Time Complexity: O(n * log m) where n is the length of source and m is the length of targetIndices (m ≤ n) Space Complexity: O(n) for the boolean array used for simulation
Solution
The solution uses a binary search over the possible number of operations (from 0 up to the length of targetIndices). For each candidate count mid, mark the first mid indices from targetIndices as removed in the source string. Then, using a two-pointers technique, check if pattern is a subsequence of the modified source string. If the pattern can be found, try a larger candidate; otherwise, reduce the candidate count. This binary search framework efficiently finds the maximum number of removals possible while ensuring the pattern remains a subsequence.