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 &lt;= k &lt;= 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 &lt;= i &lt; 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.

&nbsp;
Example 1:

Input: s = "abcacb", p = "ab", removable = [3,1,0]
Output: 2
Explanation: After removing the characters at indices 3 and 1, "abcacb" becomes "accb".
"ab" is a subsequence of "accb".
If we remove the characters at indices 3, 1, and 0, "abcacb" becomes "ccb", and "ab" is no longer a subsequence.
Hence, the maximum k is 2.


Example 2:

Input: s = "abcbddddd", p = "abcd", removable = [3,2,1,4,5,6]
Output: 1
Explanation: After removing the character at index 3, "abcbddddd" becomes "abcddddd".
"abcd" is a subsequence of "abcddddd".


Example 3:

Input: s = "abcab", p = "abc", removable = [0,1,2,3,4]
Output: 0
Explanation: If you remove the first index in the array removable, "abc" is no longer a subsequence.


&nbsp;
Constraints:


	1 &lt;= p.length &lt;= s.length &lt;= 105
	0 &lt;= removable.length &lt; s.length
	0 &lt;= removable[i] &lt; s.length
	p is a subsequence of s.
	s and p both consist of lowercase English letters.
	The elements in removable are distinct.


class Solution:
    def maximumRemovals(self, s: str, p: str, removable: List[int]) -> int:
        
        def check(m):
            i = j = 0
            remove = set(removable[:m+1])
            while i < len(s) and j < len(p):
                if i in remove:
                    i += 1
                    continue
                if s[i] == p[j]:
                    i += 1
                    j += 1
                else:
                    i += 1
            
            return j == len(p)
            
                
        # search interval is [lo, hi)
        lo, hi = 0, len(removable)+1
        
        while lo < hi:
            mid = (lo + hi) // 2
            if check(mid):
                lo = mid + 1
            else:
                hi = mid
                
        return lo if lo < len(removable) else lo-1
class Solution {
    public int maximumRemovals(String s, String p, int[] removable) {
        int left = 0, right = removable.length;

        while (left < right) {
            int middle = (right + left + 1) / 2;
            String afterRemoval = remove(s, removable, middle);
            if (isSubsequence(p, afterRemoval, p.length(), afterRemoval.length()))
                left = middle;
            else
                right = middle - 1;
        }

        return left;
    }

    private String remove(String s, int[] removable, int k) {
        char[] symbols = s.toCharArray();
        for (int i = 0; i < k; i++) {
            symbols[removable[i]] = Character.MIN_VALUE;
        }
        return String.valueOf(symbols);
    }

    private boolean isSubsequence(String subSequence, String word, int subSequenceChar, int wordChar) {
        if (subSequenceChar == 0) return true;
        if (wordChar == 0) return false;

        if (subSequence.charAt(subSequenceChar - 1) == word.charAt(wordChar - 1))
            return isSubsequence(subSequence, word, subSequenceChar - 1, wordChar - 1);

        return isSubsequence(subSequence, word, subSequenceChar, wordChar - 1);
    }
}
class Solution {
public:
    bool isSubSequence(string str1, string str2){
        int j = 0,m=str1.size(),n=str2.size();
        for (int i = 0; i < n && j < m; i++)
            if (str1[j] == str2[i])
                j++;
        return (j == m);
    }
    int maximumRemovals(string s, string p, vector<int>& removable) {
        string copy=s;
        int left = 0, right =removable.size();
        while (left <= right) {
            int mid = (left+right)/2;
            for(int i=0;i<mid;i++) copy[removable[i]]='*';
            if (isSubSequence(p,copy))
			 //if p is subsequence of string after mid number of removals then we should look for if it's possible to remove more characters 
                left = mid+1;
            else {
			//if p is not a subsequence of string it means that we have certainly removed more characters from string 
			//so we must decrease our size of removal characters and hence we  add all characters we removed earlier.
                for(int i=0;i<mid;i++) copy[removable[i]] = s[removable[i]];
                right = mid-1;
            }
        }
        return right;
    }
};
var maximumRemovals = function(s, p, removable) {
    let arr = s.split('');
    
    const stillFunctions = (k) => {
        let result = [...arr];
        for(let i = 0; i < k; i++) {
            result[removable[i]] = '';
        }

        const isSubset = () => {
            let idx = 0;
            for(let c = 0; c < p.length; c++) {
                if(idx > result.length) return false;
                const next = result.indexOf(p[c], idx);
                if(next === -1) {
                    return false;
                }
                idx = next + 1;
            }
            return true;
        }
        
        return isSubset();
    }

    let left = 0;
    let right = removable.length;
    
    while(left < right) {
        // Need to round up the midpoint since we are returning operation LENGTH i.e. ops+1
        const mid = Math.ceil((left+right) / 2);
        if(!stillFunctions(mid)) {
            right = mid -1;
        } else {
            left = mid;
        }
    }
    return left;
};
Maximum Number of Removable Characters