Skip to content

Practice Find Maximum Removals From Source String

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3316
LeetCode Problem

Find Maximum Removals From Source String

Problem Statement

Given source, a subsequence pattern, and sorted distinct targetIndices, you may remove characters only at indices in targetIndices while keeping pattern a subsequence. Indices always refer to the original source positions and do not shift after a removal. Return the maximum number of characters that can be removed.

Source: Find Maximum Removals From Source String on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
source = "abbaa", pattern = "aba", targetIndices = [0,1,2]
Output
1

# Example 2

Input
source = "bcda", pattern = "d", targetIndices = [0,3]
Output
2

# Example 3

Input
source = "dda", pattern = "dda", targetIndices = [0,1,2]
Output
0

# Example 4

Input
source = "yeyeykyded" , pattern = "yeyyd" , targetIndices = [0,2,3,4]
Output
2

Constraints

1 <= n == source.length <= 3 * 10^3
1 <= pattern.length <= n
1 <= targetIndices.length <= n
targetIndices is sorted in ascending order.
The input is generated such that targetIndices contains distinct elements in the range [0, n - 1].
source and pattern consist only of lowercase English letters.
The input is generated such that pattern appears as a subsequence in source.

Related LeetCode Problems

Practice more problems similar to Find Maximum Removals From Source String.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.