LeetCode 1898: Maximum Number of Removable Characters Solution

Master LeetCode problem 1898 (Maximum Number of Removable Characters), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1898. Maximum Number of Removable Characters

Problem Explanation

Explanation:

To solve this problem, we can use a binary search approach to find the maximum k such that p is still a subsequence of s after removing k characters. We can create a helper function that checks if p is a subsequence of s after removing k characters at the given indices. Then, we perform binary search on the range [0, removable.length] to find the maximum valid k.

Algorithmic Idea:

  1. Define a helper function isSubsequence that checks if p is a subsequence of s after removing k characters at given indices.
  2. Perform binary search on the range [0, removable.length] to find the maximum valid k.
  3. In each iteration of binary search, check if p is still a subsequence after removing mid characters at given indices.
  4. Update the search range based on the result of the helper function.

Time Complexity:

The time complexity of this approach is O(n * log(n)), where n is the length of removable.

Space Complexity:

The space complexity is O(n) for storing the indices of removable.

Solution Code

class Solution {
    public int maximumRemovals(String s, String p, int[] removable) {
        int low = 0, high = removable.length;
        
        while(low <= high) {
            int mid = low + (high - low) / 2;
            if(isSubsequence(s, p, removable, mid)) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        
        return high;
    }
    
    private boolean isSubsequence(String s, String p, int[] removable, int k) {
        StringBuilder sb = new StringBuilder(s);
        for (int i = 0; i < k; i++) {
            sb.setCharAt(removable[i], '*');
        }
        int j = 0;
        for (int i = 0; i < sb.length() && j < p.length(); i++) {
            if (sb.charAt(i) == p.charAt(j)) {
                j++;
            }
        }
        return j == p.length();
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1898 (Maximum Number of Removable Characters)?

This page provides optimized solutions for LeetCode problem 1898 (Maximum Number of Removable Characters) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1898 (Maximum Number of Removable Characters)?

The time complexity for LeetCode 1898 (Maximum Number of Removable Characters) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1898 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1898 in Java, C++, or Python.

Back to LeetCode Solutions