java.lang.Object
g1801_1900.s1898_maximum_number_of_removable_characters.Solution

public class Solution extends Object
1898 - Maximum Number of Removable Characters.<p>Medium</p> <p>You are given two strings <code>s</code> and <code>p</code> where <code>p</code> is a <strong>subsequence</strong> of <code>s</code>. You are also given a <strong>distinct 0-indexed</strong> integer array <code>removable</code> containing a subset of indices of <code>s</code> (<code>s</code> is also <strong>0-indexed</strong> ).</p> <p>You want to choose an integer <code>k</code> (<code>0 <= k <= removable.length</code>) such that, after removing <code>k</code> characters from <code>s</code> using the <strong>first</strong> <code>k</code> indices in <code>removable</code>, <code>p</code> is still a <strong>subsequence</strong> of <code>s</code>. More formally, you will mark the character at <code>s[removable[i]]</code> for each <code>0 <= i < k</code>, then remove all marked characters and check if <code>p</code> is still a subsequence.</p> <p>Return <em>the <strong>maximum</strong></em> <code>k</code> <em>you can choose such that</em> <code>p</code> <em>is still a <strong>subsequence</strong> of</em> <code>s</code> <em>after the removals</em>.</p> <p>A <strong>subsequence</strong> 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> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;abcacb&rdquo;, p = &ldquo;ab&rdquo;, removable = [3,1,0]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> After removing the characters at indices 3 and 1, &ldquo;a<strong>b</strong>c<strong>a</strong>cb&rdquo; becomes &ldquo;accb&rdquo;.</p> <p>&ldquo;ab&rdquo; is a subsequence of &ldquo;<strong>a</strong>cc<strong>b</strong>&rdquo;.</p> <p>If we remove the characters at indices 3, 1, and 0, &ldquo;<strong>ab</strong>c<strong>a</strong>cb&rdquo; becomes &ldquo;ccb&rdquo;, and &ldquo;ab&rdquo; is no longer a subsequence.</p> <p>Hence, the maximum k is 2.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;abcbddddd&rdquo;, p = &ldquo;abcd&rdquo;, removable = [3,2,1,4,5,6]</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> After removing the character at index 3, &ldquo;abc<strong>b</strong>ddddd&rdquo; becomes &ldquo;abcddddd&rdquo;.</p> <p>&ldquo;abcd&rdquo; is a subsequence of &ldquo;<strong>abcd</strong>dddd&rdquo;.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;abcab&rdquo;, p = &ldquo;abc&rdquo;, removable = [0,1,2,3,4]</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> If you remove the first index in the array removable, &ldquo;abc&rdquo; is no longer a subsequence.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= p.length <= s.length <= 10<sup>5</sup></code></li> <li><code>0 <= removable.length < s.length</code></li> <li><code>0 <= removable[i] < s.length</code></li> <li><code>p</code> is a <strong>subsequence</strong> of <code>s</code>.</li> <li><code>s</code> and <code>p</code> both consist of lowercase English letters.</li> <li>The elements in <code>removable</code> are <strong>distinct</strong>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maximumRemovals

      public int maximumRemovals(String s, String p, int[] removable)