Skip to content

[LeetCode] 1898. Maximum Number of Removable Characters #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
frdmu opened this issue Jul 16, 2021 · 0 comments
Open

[LeetCode] 1898. Maximum Number of Removable Characters #20

frdmu opened this issue Jul 16, 2021 · 0 comments

Comments

@frdmu
Copy link
Owner

frdmu commented Jul 16, 2021

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.
  • The elements in removable are distinct.

二分法,寻找第一个不满足条件的,还考到了判断子序列。代码如下:

class Solution {
public:
    bool is_subseq(string s, string p, vector<int> removable, int mid) {
        vector<char> tmp;
        string str; 
        
        for (auto c: s) {
            tmp.push_back(c);
        }     
        for (int i = 0; i <= mid; i++) {
            tmp[removable[i]] = '0'; 
        }
        for (auto e: tmp) {
            if (e != '0') {
                str += e;
            }
        }
        
        int m = str.size();
        int n = p.size();
        int i = 0, j = 0; 
        while (i < m && j < n) {
            if (str[i] == p[j]) j++;
            i++; 
        }
        
        return j == n; 
    }
    int maximumRemovals(string s, string p, vector<int>& removable) {
        int n = removable.size(); 
        int left = 0, right = n; 
        
        while (left < right) {
            int mid = left +((right - left) >> 1);
            if (is_subseq(s, p, removable, mid)) left = mid + 1;
            else right = mid; 
        }
        
        return right;
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant