Problem Description
Given two strings s and p (with p being a subsequence of s) and an array removable that lists unique indices in s, determine the maximum number k such that removing the characters at the first k indices in removable from s leaves p as a subsequence of the resulting string.
Key Insights
- Use binary search to efficiently determine the maximum k possible.
- For a given k, simulate the removal of characters from s using a set of indices from removable.
- Check if p is still a subsequence in the modified s using a two-pointer approach.
- The problem leverages checking for subsequences with removal simulation to verify a candidate k.
Space and Time Complexity
Time Complexity: O(n * log(m)) where n is the length of s and m is the length of removable.
Space Complexity: O(n) in the worst-case scenario to store removed indices.
Solution
We perform a binary search on the number of removable indices k. For each candidate k, we mark the indices from removable (from 0 to k-1) as removed. Then, by using two pointers, one iterates over s (skipping removed indices) and another iterates over p, checking if all characters of p appear in order. The binary search allows us to efficiently find the largest k such that p remains a subsequence. Key data structures include a set to store removed indices and simple pointer variables for iterating s and p.