【LeetCode】1898. Maximum number of removable characters

topic

You are given two strings s and p, where p is a subsequence of s. At the same time, you are given a removable array of integers whose elements are different from each other and whose subscripts start counting from 0. This array is a subset of the subscripts in s (the subscripts of s also start counting from 0).
Please find an integer k (0 <= k <= removable.length), select the first k subscripts in removable, and then remove the k characters corresponding to these subscripts from s. The integer k needs to satisfy: after performing the above steps, p is still a subsequence of s. A more formal explanation is that for each 0 <= i < k, first mark the character located at s[removable[i]], then remove all marked characters, and then check whether p is still a subsequence of s .
Returns the largest k you can find such that p is still a subsequence of s after removing characters.
A subsequence of a string is a new string generated from the original string, which may or may not remove some characters from the original string but does not change 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 corresponding to subscripts 3 and 1, “abcacb” becomes “accb”.
"ab" is a subsequence of "accb".
If "abcacb" becomes "ccb" after removing the characters corresponding to subscripts 3, 1 and 0, then "ab" is no longer a subsequence of s.
Therefore, the largest k is 2.

Example 2:

Input: s = “abcbddddd”, p = “abcd”, removable = [3,2,1,4,5,6]
Output: 1
Explanation: After removing the characters corresponding to subscript 3, “abcbddddd” becomes "abcdddddd" .
"abcd" is a subsequence of "abcddddd".

Example 3:

Input: s = “abcab”, p = “abc”, removable = [0,1,2,3,4]
Output: 0
Explanation: If the first subscript of the array removable is removed, “abc” is no longer is a subsequence of s.

hint:

1 <= p.length <= s.length <= 105
0 <= removable.length < s.length
0 <= removable[i] < s.length
p is a substring of s. Both
s and p are composed of lowercase English characters.
The elements in the letter composition removable are different from each other

answer

Dichotomy.
The left and right boundary values ​​are 0 and the length of the array.
Constantly change the left and right boundary values ​​to get mid. Remove mid characters and compare.

class Solution {
    
    
public:
    bool fun(string s,string p,vector<int>& nums,int mid)
    {
    
    
        for(int i=0;i<mid;i++)
        {
    
    
            s[nums[i]] = '&';//不能删除,删除就改变其他字符下标
        }
        int j=0;
        cout<<s<<endl;
        for(int i=0;i<s.length();i++)//这里不是字符串匹配而是元素匹配关系
        {
    
    
            if(s[i]==p[j])
                j++;
            if(j==p.length())
                return true;
        }
        return false;
    }
    int maximumRemovals(string s, string p, vector<int>& removable) {
    
    
        int left = 0;
        int right = removable.size();
        int res = 0;
        while(left<=right)
        {
    
    
            int mid = left + ((right-left)>>1);

            cout<<left<<"=="<<mid<<"=="<<right<<"==>";

            if(fun(s,p,removable,mid))
            {
    
    
                left = mid+1;
                res = mid;
            }
            else
            {
    
    
                right = mid-1;
            }
        }
        return res;
    }
};

Guess you like

Origin blog.csdn.net/qq_45972928/article/details/126241528