Find Maximum Removals From Source String - Problem
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:
idxis an element oftargetIndicespatternremains a subsequence ofsourceafter 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 & Output
Example 1 — Basic Case
$
Input:
source = "abcacb", pattern = "ab", targetIndices = [3,1,0]
›
Output:
2
💡 Note:
Remove characters at indices 3 and 1. After removal: "abcb" → pattern "ab" is still a subsequence.
Example 2 — Cannot Remove All
$
Input:
source = "abcabc", pattern = "abc", targetIndices = [2,1,0]
›
Output:
1
💡 Note:
Can only remove one character while keeping pattern "abc" as subsequence.
Example 3 — No Removals Possible
$
Input:
source = "abc", pattern = "abc", targetIndices = [0,1,2]
›
Output:
0
💡 Note:
Pattern uses all characters, so no characters can be removed.
Constraints
- 1 ≤ source.length ≤ 3000
- 1 ≤ pattern.length ≤ source.length
- 1 ≤ targetIndices.length ≤ source.length
- 0 ≤ targetIndices[i] < source.length
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code