The Maximum Number of Removable Characters coding problem gives you two strings, s and p, and an array of removable indices. You need to find the maximum number of characters you can remove from s (using the indices from removable) such that p remains a subsequence of the modified s.
Alphonso, Snowflake, Amazon, and Moveworks use this problem to test a candidate's proficiency with binary search on the answer, combined with a check function that involves two pointers. It's a classic pattern where the feasibility of an "answer" (number of removals) is monotonic, enabling a binary search optimization.
Binary Search on the Answer + Two Pointers Check:
The problem asks for the maximum number of removable characters. This value has a monotonic property: if we can remove k characters and p is still a subsequence, then we can certainly remove any k' < k characters and p will still be a subsequence. This monotonic property allows us to binary search for the maximum k.
The binary search will be performed on the range [0, len(removable)].
For a given mid value (representing mid characters removed):
check(mid) function:
removed_flags of size len(s), initialized to false.removed_flags[removable[i]] = true for i from 0 to mid-1. (These are the first mid characters to be removed from s).ptr_s for s and ptr_p for p.ptr_s through s. If s[ptr_s] is not marked as removed_flags[ptr_s] and s[ptr_s] == p[ptr_p], then increment ptr_p.ptr_p reaches len(p), it means p is a subsequence. Return true.ptr_s reaches len(s) but ptr_p has not reached len(p), return false.Binary Search Logic:
low = 0, high = len(removable), ans = 0.low <= high:
mid = low + (high - low) // 2.check(mid) is true: it means mid characters can be removed. Try to remove more: ans = mid, low = mid + 1.check(mid) is false: mid characters cannot be removed. Try removing fewer: high = mid - 1.ans.Time Complexity: O(len(removable) * (len(s) + len(p))). The log(len(removable)) factor comes from binary search, and len(s) + len(p) from the two-pointer check. Note: len(removable) for creating removed_flags is the dominant factor in check unless len(s) is much larger.
s = "abcacb", p = "ab", removable = [3, 1, 0].
len(removable) = 3. Binary search [0, 3].
low = 0, high = 3, ans = 0.
mid = 1: removable_indices = {3}.
s after removing s[3]: "ab_acb" -> "abacb".
Is "ab" a subsequence of "abacb"? Yes.
ans = 1, low = 2.
mid = 2: removable_indices = {3, 1}.
s after removing s[3], s[1]: "a_a_cb" -> "aacb".
Is "ab" a subsequence of "aacb"?
ptr_s at 'a' (idx 0, not removed), ptr_p at 'a' (idx 0). Match. ptr_p++.
ptr_s at 'a' (idx 2, not removed), ptr_p at 'b' (idx 1). No match.
ptr_s at 'c' (idx 4, not removed), ptr_p at 'b' (idx 1). No match.
ptr_s at 'b' (idx 5, not removed), ptr_p at 'b' (idx 1). Match. ptr_p++.
ptr_p is 2, which is len(p). Yes.
ans = 2, low = 3.
mid = 3: removable_indices = {3, 1, 0}.
s after removing s[3], s[1], s[0]: "__a_cb" -> "acb".
Is "ab" a subsequence of "acb"?
ptr_s at 'a' (idx 2, not removed), ptr_p at 'a' (idx 0). Match. ptr_p++.
ptr_s at 'c' (idx 4, not removed), ptr_p at 'b' (idx 1). No match.
ptr_s at 'b' (idx 5, not removed), ptr_p at 'b' (idx 1). Match. ptr_p++.
ptr_p is 2, which is len(p). Yes.
ans = 3, low = 4.
Loop terminates (low=4, high=3). ans = 3.
check function: The core of binary search on answer problems. Ensure the check function correctly determines feasibility for the given mid value. This includes accurately handling character removals and subsequence checks.low = mid + 1 and high = mid - 1 are crucial for converging.check: The check function itself must be efficient (e.g., O(len(s) + len(p))).For the Array Two Pointers Binary Search String interview pattern, problems often reduce to a check function with two pointers or similar linear scans, wrapped in a binary search. Practice recognizing monotonic properties and designing efficient check functions.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Sentence Similarity III | Medium | Solve | |
| Two Sum II - Input Array Is Sorted | Medium | Solve | |
| Expressive Words | Medium | Solve | |
| Maximum Distance Between a Pair of Values | Medium | Solve | |
| Find First Palindromic String in the Array | Easy | Solve |