Class Solution
- java.lang.Object
-
- g1801_1900.s1898_maximum_number_of_removable_characters.Solution
-
public class Solution extends Object
1898 - Maximum Number of Removable Characters.Medium
You are given two strings
s
andp
wherep
is a subsequence ofs
. You are also given a distinct 0-indexed integer arrayremovable
containing a subset of indices ofs
(s
is also 0-indexed ).You want to choose an integer
k
(0 <= k <= removable.length
) such that, after removingk
characters froms
using the firstk
indices inremovable
,p
is still a subsequence ofs
. More formally, you will mark the character ats[removable[i]]
for each0 <= i < k
, then remove all marked characters and check ifp
is still a subsequence.Return the maximum
k
you can choose such thatp
is still a subsequence ofs
after the removals.A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
Example 1:
Input: s = “abcacb”, p = “ab”, removable = [3,1,0]
Output: 2
Explanation: After removing the characters at indices 3 and 1, “abcacb” becomes “accb”.
“ab” is a subsequence of “accb”.
If we remove the characters at indices 3, 1, and 0, “abcacb” becomes “ccb”, and “ab” is no longer a subsequence.
Hence, the maximum k is 2.
Example 2:
Input: s = “abcbddddd”, p = “abcd”, removable = [3,2,1,4,5,6]
Output: 1
Explanation: After removing the character at index 3, “abcbddddd” becomes “abcddddd”.
“abcd” is a subsequence of “abcddddd”.
Example 3:
Input: s = “abcab”, p = “abc”, removable = [0,1,2,3,4]
Output: 0
Explanation: If you remove the first index in the array removable, “abc” is no longer a subsequence.
Constraints:
1 <= p.length <= s.length <= 105
0 <= removable.length < s.length
0 <= removable[i] < s.length
p
is a subsequence ofs
.s
andp
both consist of lowercase English letters.- The elements in
removable
are distinct.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-