O(N) java solution matching from prefix and suffix then sliding window.
Code


# Code
```java []
class Solution {
    public int minimumScore(String s, String t) {
        /*so want to select a substring of t and only remove chars from said substring
        then find the smallest substring of t possible to do this s.t. t becomes a subsequence of s.
        Note: if removing some elements from a substring, might as well remove all as that would make it a subsequence
        of removing some which if removing any amount gets a subsequence of s, by transitivity this would also.
        
        Therefore, problem is equivalent to finding the length of the smallest possible substring you can remove 
        from t s.t. t becomes a subsequence of s.
        */
        int m = t.length(), n = s.length();
        int[] prefixMatch = new int[m]; //prefixMatch[i] gives s index matching t[i] where a subseq exists for prefix before t[i] in s before index
        int[] suffixMatch = new int[m];
        Arrays.fill(prefixMatch, (int)1e9);
        Arrays.fill(suffixMatch, -n);

        int sPtr = 0, tPtr = 0, res = m;
        while(sPtr < n && tPtr < m) {
            if(s.charAt(sPtr) == t.charAt(tPtr)) {
                prefixMatch[tPtr] = sPtr;
                tPtr++;
            }
            sPtr++;
        }

        if(tPtr == m) return 0;
        res = m-tPtr;

        sPtr = n-1;
        tPtr = m-1;
        while(sPtr >= 0 && tPtr >= 0) {
            if(s.charAt(sPtr) == t.charAt(tPtr)) {
                suffixMatch[tPtr] = sPtr;
                tPtr--;
            }
            sPtr--;
        }

        res = Math.min(res, tPtr+1);


        int left = 0;
        for(int right = 0; right < m; right++) {
            if(suffixMatch[right] > prefixMatch[left]) {
                while(suffixMatch[right] > prefixMatch[left++]);
                res = Math.min(res, right-left+1);
            }
        }

        return res;
    }
}
```
['Java']