{"id":8567,"date":"2021-08-11T19:40:40","date_gmt":"2021-08-12T02:40:40","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8567"},"modified":"2021-08-11T19:41:52","modified_gmt":"2021-08-12T02:41:52","slug":"leetcode-1898-maximum-number-of-removable-characters","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-1898-maximum-number-of-removable-characters\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1898. Maximum Number of Removable Characters"},"content":{"rendered":"\n<p>You are given two strings&nbsp;<code>s<\/code>&nbsp;and&nbsp;<code>p<\/code>&nbsp;where&nbsp;<code>p<\/code>&nbsp;is a&nbsp;<strong>subsequence&nbsp;<\/strong>of&nbsp;<code>s<\/code>. You are also given a&nbsp;<strong>distinct 0-indexed&nbsp;<\/strong>integer array&nbsp;<code>removable<\/code>&nbsp;containing a subset of indices of&nbsp;<code>s<\/code>&nbsp;(<code>s<\/code>&nbsp;is also&nbsp;<strong>0-indexed<\/strong>).<\/p>\n\n\n\n<p>You want to choose an integer&nbsp;<code>k<\/code>&nbsp;(<code>0 &lt;= k &lt;= removable.length<\/code>) such that, after removing&nbsp;<code>k<\/code>&nbsp;characters from&nbsp;<code>s<\/code>&nbsp;using the&nbsp;<strong>first<\/strong>&nbsp;<code>k<\/code>&nbsp;indices in&nbsp;<code>removable<\/code>,&nbsp;<code>p<\/code>&nbsp;is still a&nbsp;<strong>subsequence<\/strong>&nbsp;of&nbsp;<code>s<\/code>. More formally, you will mark the character at&nbsp;<code>s[removable[i]]<\/code>&nbsp;for each&nbsp;<code>0 &lt;= i &lt; k<\/code>, then remove all marked characters and check if&nbsp;<code>p<\/code>&nbsp;is still a subsequence.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;<\/em><code>k<\/code><em>&nbsp;you can choose such that&nbsp;<\/em><code>p<\/code><em>&nbsp;is still a&nbsp;<strong>subsequence<\/strong>&nbsp;of&nbsp;<\/em><code>s<\/code><em>&nbsp;after the removals<\/em>.<\/p>\n\n\n\n<p>A&nbsp;<strong>subsequence<\/strong>&nbsp;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.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"abcacb\", p = \"ab\", removable = [3,1,0]\n<strong>Output:<\/strong> 2\n<strong>Explanation<\/strong>: After removing the characters at indices 3 and 1, \"a<s><strong>b<\/strong><\/s>c<s><strong>a<\/strong><\/s>cb\" becomes \"accb\".\n\"ab\" is a subsequence of \"<strong><u>a<\/u><\/strong>cc<strong><u>b<\/u><\/strong>\".\nIf we remove the characters at indices 3, 1, and 0, \"<s><strong>ab<\/strong><\/s>c<s><strong>a<\/strong><\/s>cb\" becomes \"ccb\", and \"ab\" is no longer a subsequence.\nHence, the maximum k is 2.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"abcbddddd\", p = \"abcd\", removable = [3,2,1,4,5,6]\n<strong>Output:<\/strong> 1\n<strong>Explanation<\/strong>: After removing the character at index 3, \"abc<s><strong>b<\/strong><\/s>ddddd\" becomes \"abcddddd\".\n\"abcd\" is a subsequence of \"<strong>abcd<\/strong>dddd\".\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"abcab\", p = \"abc\", removable = [0,1,2,3,4]\n<strong>Output:<\/strong> 0\n<strong>Explanation<\/strong>: If you remove the first index in the array removable, \"abc\" is no longer a subsequence.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= p.length &lt;= s.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= removable.length &lt; s.length<\/code><\/li><li><code>0 &lt;= removable[i] &lt; s.length<\/code><\/li><li><code>p<\/code>&nbsp;is a&nbsp;<strong>subsequence<\/strong>&nbsp;of&nbsp;<code>s<\/code>.<\/li><li><code>s<\/code>&nbsp;and&nbsp;<code>p<\/code>&nbsp;both consist of lowercase English letters.<\/li><li>The elements in&nbsp;<code>removable<\/code>&nbsp;are&nbsp;<strong>distinct<\/strong>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Binary Search + Two Pointers<\/strong><\/h2>\n\n\n\n<p>If we don&#8217;t remove any thing, p is a subseq of s, as we keep removing, at some point L, p is no longer a subseq of s. e.g [0:True, 1: True, &#8230;, L &#8211; 1: True, L: False, L+1: False, &#8230;, m:False], this array is monotonic. We can use binary search to find the smallest L such that p is no long a subseq of s. Ans = L &#8211; 1.<\/p>\n\n\n\n<p>For each guess, we can use two pointers to check whether p is subseq of removed(s) in O(n).<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maximumRemovals(string s, string p, vector<int>& removable) {\n    const int n = s.length();\n    const int t = p.length();\n    const int m = removable.size();    \n    int l = 0;\n    int r = m + 1;\n    vector<int> idx(n, INT_MAX);\n    for (int i = 0; i < m; ++i)\n      idx[removable[i]] = i;\n    while (l < r) {\n      int mid = l + (r - l) \/ 2;\n      int j = 0;\n      for (int i = 0; i < n &#038;&#038; j < t; ++i)\n        if (idx[i] >= mid && s[i] == p[j]) ++j;      \n      if (j != t)\n        r = mid;\n      else\n        l = mid + 1;\n    }\n    \/\/ l is the smallest number s.t. p is no longer a subseq of s.\n    return l - 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two strings&nbsp;s&nbsp;and&nbsp;p&nbsp;where&nbsp;p&nbsp;is a&nbsp;subsequence&nbsp;of&nbsp;s. You are also given a&nbsp;distinct 0-indexed&nbsp;integer array&nbsp;removable&nbsp;containing a subset of indices of&nbsp;s&nbsp;(s&nbsp;is also&nbsp;0-indexed). You want to choose an integer&nbsp;k&nbsp;(0&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[52,177,229,175],"class_list":["post-8567","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-medium","tag-subsequence","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8567"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8567\/revisions"}],"predecessor-version":[{"id":8569,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8567\/revisions\/8569"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}