You are given two strings s
and p
where p
is a subsequence of s
. You are also given a distinct 0-indexed integer array removable
containing a subset of indices of s
(s
is also 0-indexed).
You want to choose an integer k
(0 <= k <= removable.length
) such that, after removing k
characters from s
using the first k
indices in removable
, p
is still a subsequence of s
. More formally, you will mark the character at s[removable[i]]
for each 0 <= i < k
, then remove all marked characters and check if p
is still a subsequence.
Return the maximum k
you can choose such that p
is still a subsequence of s
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.
Companies:
alphonso
Related Topics:
Binary Search
This problem has monotonicity in it -- there must be a k
that for all i <= k
, removing the first i
characters is valid; for all i > k
, removing the first i
characters is invalid.
We can use binary search to search this maximum valid k
.
The search range is [0, N]
.
We just need to define a valid(k)
function which can be done in O(N)
time..
// OJ: https://leetcode.com/problems/maximum-number-of-removable-characters/
// Author: github.com/lzl124631x
// Time: O(NlogN)
// Space: O(N)
class Solution {
int rm[100001] = {};
bool valid(string &s, string &p, vector<int> &A, int k) {
memset(rm, 0, sizeof(rm));
for (int i = 0; i < k; ++i) rm[A[i]] = 1; // mark this index as removed
int N = s.size(), M = p.size(), j = 0; // `j` is the number of characters in `p` we matched with `s`
for (int i = 0; i < N && j < M; ++i) {
if (rm[i] == 1) continue; // this character is removed, skip
if (s[i] == p[j]) ++j; // found a match, increment `j`
}
return j == M; // if `j` reaches the end of `p`, `p` is still a subsequence of `s`.
}
public:
int maximumRemovals(string s, string p, vector<int>& A) {
int L = 0, R = A.size();
while (L <= R) {
int M = (L + R) / 2;
if (valid(s, p, A, M)) L = M + 1;
else R = M - 1;
}
return R;
}
};