Maximum Number of Removable Characters - Problem

Imagine you have a string puzzle where you need to maintain a specific pattern! You're given two strings: a main string s and a pattern string p, where p is guaranteed to be a subsequence of s.

Here's the challenge: You also have an array removable containing indices that specify which characters you can remove from s, and in what order you must remove them. Your goal is to find the maximum number of characters you can remove while still keeping p as a subsequence of the remaining string.

Key Rules:

  • You must remove characters in the order specified by the removable array
  • After removals, p must still be a subsequence of the modified s
  • A subsequence maintains the relative order of characters (but doesn't need to be contiguous)

Example: If s = "abcacb", p = "ab", and removable = [3,1,0], you can remove up to 2 characters and still maintain "ab" as a subsequence!

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "abcacb", p = "ab", removable = [3,1,0]
โ€บ Output: 2
๐Ÿ’ก Note: After removing indices 3 and 1 (first 2 from removable), s becomes "a_c_cb" โ†’ "accb". The subsequence "ab" can still be found as "a" (index 0) and "b" (index 4). If we remove index 0 too, we get "ccb" and can't find "ab" as a subsequence.
example_2.py โ€” All Characters Removable
$ Input: s = "abcbddddd", p = "abcd", removable = [4,5,6,7,8]
โ€บ Output: 5
๐Ÿ’ก Note: We can remove all characters in removable ([4,5,6,7,8]) since they are all 'd' characters that come after our pattern "abcd". The string becomes "abcb" and "abcd" is still a subsequence using indices [0,1,2,3] โ†’ "abc" + "b".
example_3.py โ€” No Removals Possible
$ Input: s = "abcab", p = "abc", removable = [0,1,2,3,4]
โ€บ Output: 0
๐Ÿ’ก Note: The pattern "abc" requires characters at indices 0,1,2. Since removable[0] = 0, removing even one character (the first 'a') breaks the subsequence. Therefore, we can remove 0 characters maximum.

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 only lowercase English letters
  • removable contains distinct integers

Visualization

Tap to expand
๐Ÿ“š Library Book Removal ProblemLibrary Shelf (String s)abcacbReading List (Pattern p)abRemoval Orders310๐Ÿ” Binary Search Process:k = 0,1,2,3Test k=2โœ“ WorksAfter removing k=2 books (indices 3,1):abcacbRemaining books: "a_c_cb" โ†’ Can still find "ab" sequence!๐Ÿ’ก Key Insight: Monotonic PropertyIf we can remove k books and keep the reading sequence,we can definitely remove any number โ‰ค k books too!
Understanding the Visualization
1
Library Setup
You have books on a shelf (string s) and a reading list (pattern p) that must be found in order
2
Removal Orders
Librarian gives you removal orders (removable array) - you must follow this sequence
3
Binary Search
Instead of trying each removal count, use binary search to efficiently find the maximum
4
Subsequence Check
For each candidate removal count, verify if reading list is still achievable
Key Takeaway
๐ŸŽฏ Key Insight: The monotonic property (if k works, then k-1 works too) allows us to binary search the answer instead of trying every possibility, reducing time complexity from O(nยฒ) to O(n log n).
Asked in
Google 42 Amazon 38 Microsoft 24 Meta 18
68.4K Views
High Frequency
~18 min Avg. Time
1.8K 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