标签:ted dice 直接 target out ini 步骤 著作权 ==
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 of s
.s
and p
both consist of lowercase English letters.removable
are distinct.可移除字符的最大数目。
给你两个字符串 s 和 p ,其中 p 是 s 的一个 子序列 。同时,给你一个元素 互不相同 且下标 从 0 开始 计数的整数数组 removable ,该数组是 s 中下标的一个子集(s 的下标也 从 0 开始 计数)。
请你找出一个整数 k(0 <= k <= removable.length),选出 removable 中的 前 k 个下标,然后从 s 中移除这些下标对应的 k 个字符。整数 k 需满足:在执行完上述步骤后, p 仍然是 s 的一个 子序列 。更正式的解释是,对于每个 0 <= i < k ,先标记出位于 s[removable[i]] 的字符,接着移除所有标记过的字符,然后检查 p 是否仍然是 s 的一个子序列。
返回你可以找出的 最大 k ,满足在移除字符后 p 仍然是 s 的一个子序列。
字符串的一个 子序列 是一个由原字符串生成的新字符串,生成过程中可能会移除原字符串中的一些字符(也可能不移除)但不改变剩余字符之间的相对顺序。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-number-of-removable-characters
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题意不难理解,我这里提几个需要注意的点。首先注意限制条件的最后一项,the elements in removable are distinct。这句话的意思是被移除的字母的下标在原字符串中是唯一的,而且这个下标是对应到这个字母最一开始在字符串中的下标的。我举个例子,比如 input 字符串是 abcde ,removable = [0, 1, 2] 好了。如果我们一开始移除了 a(index == 0),这个移除操作不能影响之后的字母,也就是说我们移除了字母 a 之后,之后被移除的下标 index == 1 和 index == 2,对应的依然是字母 b 和 c。所以我们没法利用类似 stack 的数据结构来做这个移除的工作。
其次,这个题判断 移除若干字母过后的子串 和 字符串p 是否是子序列关系,我们还是需要利用到392题的思路,这里的时间复杂度没法降低了。同时这道题我一开始根据题意模拟整个过程,removable数组里面的下标一个个去删除,删除完了之后去判断 isSubsequence,最后会超时,我这里直接给出二分的解法。
这道题的二分,分的是 removable 数组,也就是说我一次性就移除很多个字母,如果 p 不是移除之后的字符串的子序列,那么就移动左指针,说明移除的东西太多了;反之则移动右指针,说明有可能移除了更多的字母之后,p 依然是移除之后的字符串的子序列。注意代码16行,需要一直遍历到 mid。
时间O(nlogn)
空间O(n)
Java实现
1 class Solution { 2 public int maximumRemovals(String s, String p, int[] removable) { 3 // corner case 4 if (removable == null || removable.length == 0) { 5 return 0; 6 } 7 8 // normal case 9 int len = s.length(); 10 int left = 0; 11 int right = removable.length - 1; 12 while (left <= right) { 13 int mid = left + (right - left) / 2; 14 int[] indexes = new int[len]; 15 // 要一直遍历到mid,这里很容易写错 16 for (int i = 0; i <= mid; i++) { 17 indexes[removable[i]] = 1; 18 } 19 20 StringBuilder sb = new StringBuilder(); 21 for (int j = 0; j < len; j++) { 22 if (indexes[j] == 0) { 23 sb.append(s.charAt(j)); 24 } 25 } 26 if (isSubsequence(sb.toString(), p)) { 27 left = mid + 1; 28 } else { 29 right = mid - 1; 30 } 31 } 32 return right + 1; 33 } 34 35 private boolean isSubsequence(String s, String p) { 36 int i = 0; 37 int j = 0; 38 while (i < s.length() && j < p.length()) { 39 if (s.charAt(i) == p.charAt(j)) { 40 i++; 41 j++; 42 } else { 43 i++; 44 } 45 } 46 return j == p.length() ? true : false; 47 } 48 }
[LeetCode] 1898. Maximum Number of Removable Characters
标签:ted dice 直接 target out ini 步骤 著作权 ==
原文地址:https://www.cnblogs.com/cnoodle/p/14883873.html