Maximum Number of Removable Characters - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: s = "abcacb", p = "ab", removable = [3,1,0]
Output: 2
💡 Note: After removing indices 3 and 1: s becomes "a_c_cb" → "accb", which still contains "ab" as subsequence
Example 2 — Single Character
$ Input: s = "abcbddddd", p = "abcd", removable = [3,2,1,4,7,6]
Output: 1
💡 Note: Remove index 3 to get "abc_ddddd" → "abcddddd", which still contains "abcd" as subsequence. Removing index 2 as well would eliminate 'c', breaking the subsequence.
Example 3 — No Removals Possible
$ Input: s = "abcab", p = "abc", removable = [0,1,2]
Output: 0
💡 Note: Removing any character breaks the subsequence, so maximum k = 0

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 consist of lowercase English letters
  • The elements in removable are distinct

Visualization

Tap to expand
Maximum Number of Removable Characters INPUT String s = "abcacb" a 0 b 1 c 2 a 3 c 4 b 5 Pattern p = "ab" a b removable = [3, 1, 0] 3 k=1 1 k=2 0 k=3 Find max k where p is still subsequence of s after removing k chars Range: 0 <= k <= 3 ALGORITHM - Binary Search 1 Binary Search Setup left=0, right=3, mid=1 2 Check k=1 (remove idx 3) s="abc_cb" has "ab"? OK 3 Check k=2 (remove 3,1) s="a_c_cb" has "ab"? OK 4 Check k=3 (remove 3,1,0) s="__c_cb" has "ab"? NO Binary Search Progress k=0 k=1 k=2 k=3 OK OK OK NO Max k = 2 FINAL RESULT After k=2 removals: a b c a c b Removed indices: 3, 1 Remaining: "a_c_cb" --> "accb" Subsequence Check "ab" in "accb"? OK a--c--c--b (a at 0, b at 3) Output k = 2 Maximum removals while keeping p as subsequence Time: O(n log m) Key Insight: The problem has monotonic property: if k removals work, then k-1 also works. If k fails, k+1 also fails. This allows binary search on k. For each candidate k, mark first k indices as removed and check if p is still a subsequence using two-pointer technique. Binary search gives O(n log m) vs O(nm) brute force. TutorialsPoint - Maximum Number of Removable Characters | Binary Search Approach
Asked in
Google 15 Facebook 12 Amazon 8
28.4K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen