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.
Input: source ="abbaa", pattern ="aba", targetIndices =[0,1,2]Output: 1Explanation:
We can't remove `source[0]` but we can do either of these two operations:* Remove `source[1]`, so that `source` becomes `"a_baa"`.* Remove `source[2]`, so that `source` becomes `"ab_aa"`.
Input: source ="dda", pattern ="dda", targetIndices =[0,1,2]Output: 0Explanation:
We can't remove any character from `source`.#### Example 4Input: source ="yeyeykyded", pattern ="yeyyd", targetIndices =[0,2,3,4]Output: 2Explanation:
We can remove `source[2]` and `source[3]`in two operations.
We want to maximize the number of removals from source at indices in targetIndices such that pattern remains a subsequence. Since each removal is independent and the order of removals doesn’t affect the result, we can use binary search to find the maximum number of removable indices.
classSolution {
public:int maximumRemovals(string source, string pattern, vector<int>& targetIndices) {
int l =0, r = targetIndices.size(), ans =0;
while (l <= r) {
int mid = l + (r-l)/2;
vector<bool> removed(source.size(), false);
for (int i =0; i < mid; ++i) removed[targetIndices[i]] = true;
int j =0;
for (int i =0; i < source.size() && j < pattern.size(); ++i) {
if (!removed[i] && source[i] == pattern[j]) ++j;
}
if (j == pattern.size()) {
ans = mid;
l = mid +1;
} else {
r = mid -1;
}
}
return ans;
}
};
classSolution {
publicintmaximumRemovals(String source, String pattern, int[] targetIndices) {
int l = 0, r = targetIndices.length, ans = 0;
while (l <= r) {
int mid = l + (r-l)/2;
boolean[] removed =newboolean[source.length()];
for (int i = 0; i < mid; i++) removed[targetIndices[i]]=true;
int j = 0;
for (int i = 0; i < source.length() && j < pattern.length(); i++) {
if (!removed[i]&& source.charAt(i) == pattern.charAt(j)) j++;
}
if (j == pattern.length()) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return ans;
}
}
classSolution {
funmaximumRemovals(source: String, pattern: String, targetIndices: IntArray): Int {
var l = 0; var r = targetIndices.size; var ans = 0while (l <= r) {
val mid = l + (r-l)/2val removed = BooleanArray(source.length)
for (i in0 until mid) removed[targetIndices[i]] = truevar j = 0for (i in0 until source.length) {
if (j < pattern.length && !removed[i] && source[i] == pattern[j]) j++ }
if (j == pattern.length) {
ans = mid
l = mid + 1 } else {
r = mid - 1 }
}
return ans
}
}
classSolution:
defmaximumRemovals(self, source: str, pattern: str, targetIndices: list[int]) -> int:
defis_subseq(k: int) -> bool:
removed = set(targetIndices[:k])
j =0for i, c in enumerate(source):
if i in removed:
continueif j < len(pattern) and c == pattern[j]:
j +=1return j == len(pattern)
l, r, ans =0, len(targetIndices), 0while l <= r:
mid = (l + r) //2if is_subseq(mid):
ans = mid
l = mid +1else:
r = mid -1return ans
impl Solution {
pubfnmaximum_removals(source: String, pattern: String, target_indices: Vec<i32>) -> i32 {
let s = source.as_bytes();
let p = pattern.as_bytes();
let n = s.len();
letmut l =0;
letmut r = target_indices.len();
letmut ans =0;
while l <= r {
let mid = l + (r-l)/2;
letmut removed =vec![false; n];
for&idx in&target_indices[..mid.min(target_indices.len())] {
removed[idx asusize] =true;
}
letmut j =0;
for i in0..n {
if removed[i] { continue; }
if j < p.len() && s[i] == p[j] { j +=1; }
}
if j == p.len() {
ans = mid;
l = mid +1;
} else {
r = mid -1;
}
}
ans asi32 }
}
⏰ Time complexity: O(n log m), where n is the length of source and m is the length of targetIndices. Each subsequence check is O(n), and binary search is O(log m).
🧺 Space complexity: O(n), for the removed indices set or array.