最小覆盖子串

给出两个字符串 s 和 t,要求在 s 中找出最短的包含 t 中所有字符的连续子串。

image.png

思路:
双指针方法 l,r两个指针
1、先移动r,判断r-l字符串中是否包含T,如果包含记录字符串其实位置、终点位置和长度
2、包含计算后,可以移动l,移动到count>0,

import java.util.*;


public class Solution {
    /**
     * 
     * @param S string字符串 
     * @param T string字符串 
     * @return string字符串
     */
    public String minWindow (String S, String T) {
         // write code here
        int [] map = new int[128];
       // 初始化T 字符,在map对应位置++
        for(int i = 0;i0){
                count --;
            }
            while(count == 0){
                if(r-l= 0 说明匹配过,未匹配应该负数
                if(map[S.charAt(l++)]++ >= 0){
                    count ++;
                }
            }
        }
        if(length == Integer.MAX_VALUE){
            return "";
        }
        return S.substring(head,head+length);
    }
}

你可能感兴趣的:(最小覆盖子串)