Find Maximum Removals From Source String Problem
Description
LeetCode Problem 3316.
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 oftargetIndices.patternremains 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.
Example 1:
Input: source = "abbaa", pattern = "aba", targetIndices = [0,1,2]
Output: 1
Explanation:
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".
Example 2:
Input: source = "bcda", pattern = "d", targetIndices = [0,3]
Output: 2
Explanation:
We can remove source[0] and source[3] in two operations.
Example 3:
Input: source = "dda", pattern = "dda", targetIndices = [0,1,2]
Output: 0
Explanation:
We can't remove any character from source.
Example 4:
Input: source = "yeyeykyded", pattern = "yeyyd", targetIndices = [0,2,3,4]
Output: 2
Explanation:
We can remove source[2] and source[3] in two operations.
Constraints:
1 <= n == source.length <= 3 * 10^31 <= pattern.length <= n1 <= targetIndices.length <= ntargetIndicesis sorted in ascending order.- The input is generated such that
targetIndicescontains distinct elements in the range[0, n - 1]. sourceandpatternconsist only of lowercase English letters.- The input is generated such that
patternappears as a subsequence insource.
Solution
Approach: Dynamic Programming
Let f[i][j] be the largest number of removals possible among the first i characters of source, given that the first j characters of pattern are matched using those i characters. Start with f[0][0] = 0 and every other state unreachable (negative infinity).
For each character source[i - 1] there are two options:
- Do not use it for matching: it is kept as filler, and if
i - 1is intargetIndiceswe can remove it, sof[i][j] = f[i - 1][j] + (1 if i - 1 is a target index else 0). - Use it as
pattern[j - 1], which needssource[i - 1] == pattern[j - 1]: thenf[i][j] = max(f[i][j], f[i - 1][j - 1]). A matched character is never removed.
The answer is f[m][n], where m and n are the lengths of source and pattern.
Complexity: O(m * n) time and O(m * n) space.
from math import inf
class Solution:
def maxRemovals(self, source: str, pattern: str, targetIndices: List[int]) -> int:
m, n = len(source), len(pattern)
f = [[-inf] * (n + 1) for _ in range(m + 1)]
f[0][0] = 0
s = set(targetIndices)
for i, c in enumerate(source, 1):
for j in range(n + 1):
f[i][j] = f[i - 1][j] + int((i - 1) in s)
if j and c == pattern[j - 1]:
f[i][j] = max(f[i][j], f[i - 1][j - 1])
return f[m][n]