Find Maximum Removals From Source String - Problem
Find Maximum Removals From Source String

You're given a string source of size n and a string pattern that is guaranteed to be a subsequence of source. You also have a sorted integer array targetIndices containing distinct indices in the range [0, n-1].

Your goal is to maximize the number of characters you can remove from source while ensuring that pattern remains a subsequence of the modified string.

Rules:
• You can only remove characters at indices present in targetIndices
• After each removal, pattern must still be a subsequence of source
• Character indices don't shift after removals (e.g., removing 'c' from "acb" keeps 'b' at index 2)

Example: If source = "abbaa", pattern = "aba", and targetIndices = [0, 1, 2], you could remove characters at indices 1 and 2, giving you "a_a" which still contains "aba" as a subsequence.

Input & Output

example_1.py — Basic Case
$ Input: source = "abbaa", pattern = "aba", targetIndices = [0, 1, 2]
Output: 2
💡 Note: We can remove characters at indices 0 and 1. The resulting string "_baa" (where _ represents removed chars) still contains "aba" as a subsequence: 'a' from index 4, 'b' from index 2, 'a' from index 3.
example_2.py — No Removals Possible
$ Input: source = "bcda", pattern = "d", targetIndices = [0, 3]
Output: 1
💡 Note: We can remove the character at index 0 ('b'), but we cannot remove the character at index 3 ('d') because it's needed for the pattern. So we can remove at most 1 character.
example_3.py — All Removable
$ Input: source = "yeyeyeyew", pattern = "yeye", targetIndices = [5, 6, 7, 8]
Output: 4
💡 Note: The pattern "yeye" can be formed using characters at indices 0,1,2,3. All characters at indices 5,6,7,8 can be removed since they're not needed for the pattern.

Constraints

  • 1 ≤ source.length ≤ 3 × 103
  • 1 ≤ pattern.length ≤ source.length
  • 1 ≤ targetIndices.length ≤ source.length
  • targetIndices is sorted in ascending order
  • The integers in targetIndices are distinct
  • 0 ≤ targetIndices[i] < source.length
  • pattern is guaranteed to be a subsequence of source
  • source and pattern consist only of lowercase English letters

Visualization

Tap to expand
String Subsequence PreservationOriginal String: "abbaa"a0b1b2a3a4Target Indices: [0, 1, 2] (Red boxes)Pattern: "aba" (need to preserve)After Optimal Removals:removedremovedbkeptakeptakeptRemaining: "baa" → Contains "aba" subsequence ✓Maximum removals: 2Key Strategy1. Calculate minimum chars needed (DP)2. Greedily remove when safe3. Preserve subsequence property
Understanding the Visualization
1
Analyze Requirements
Build DP table to understand minimum characters needed at each position
2
Make Greedy Choices
At each target position, decide if removal is safe based on remaining needs
3
Preserve Pattern
Ensure the pattern remains a valid subsequence after each removal
4
Maximize Removals
Count the total number of safe removals possible
Key Takeaway
🎯 Key Insight: Dynamic programming tells us the minimum characters needed from each position, enabling safe greedy removal decisions that maximize deletions while preserving the subsequence property.
Asked in
Google 45 Meta 32 Amazon 28 Microsoft 25
38.2K Views
Medium-High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen