Class Solution
-
- All Implemented Interfaces:
public final class Solution3316 - Find Maximum Removals From Source String.
Medium
You are given a string
sourceof sizen, a stringpatternthat is a subsequence ofsource, and a sorted integer arraytargetIndicesthat contains distinct numbers in the range[0, n - 1].We define an operation as removing a character at an index
idxfromsourcesuch 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 thatsourcebecomes"a_baa".Remove
source[2], so thatsourcebecomes"ab_aa".
Example 2:
Input: source = "bcda", pattern = "d", targetIndices = 0,3
Output: 2
Explanation:
We can remove
source[0]andsource[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]andsource[3]in two operations.Constraints:
<code>1 <= n == source.length <= 3 * 10<sup>3</sup></code>
1 <= 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.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegermaxRemovals(String source, String pattern, IntArray targetIndices)-
-
Method Detail
-
maxRemovals
final Integer maxRemovals(String source, String pattern, IntArray targetIndices)
-
-
-
-