Maximum Number of Removable Characters

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 where 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 whether 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, possibly none, deleted without changing the relative order of the remaining characters.

Example 1
Inputs = "abcacb", p = "ab", removable = [3,1,0]
Output2
After removing indices 3 and 1, p is still a subsequence, but removing indices 3, 1, and 0 makes p no longer a subsequence, so the maximum k is 2.
Example 2
Inputs = "abcbddddd", p = "abcd", removable = [3,2,1,4,5,6]
Output1
After removing the character at index 3, abcd is still a subsequence of the remaining string.

Constraints

  • 1 <= p.length <= s.length <= 10^5
  • 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.

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate