1898. Maximum Number of Removable Characters
https://leetcode.com/problems/maximum-number-of-removable-characters/
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:
Example 2:
Example 3:
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.
对字符串s,removeable中元素值为s的元素index,表示s中对应序号的字符将被删除,问对removable从前往后最多能取多少个index删除,使得p依然是s的子序列。暴力是从头往后依次检查removable是否满足条件,O(N^2)。进一步想如果removable某元素m满足条件,那m之前的已经包括在里头,肯定都满足=>二分,O(NlgN)。
Last updated
Was this helpful?