Leetcode76:最小覆盖子串

题目76:最小覆盖子串

解法1 滑动窗口

	/*
    首先想到使用滑动窗口
     */
    public String minWindow(String s, String t) {

        int[] freq = new int[128];
        int count = 0;
        int minLen = Integer.MAX_VALUE;
        int left = 0;
        int right = 0;
        String ret = "";
        for (int i = 0 ; i< t.length(); i++){
            freq[t.charAt(i)]++;
	}
	while (right<s.length()){
	    if (--freq[s.charAt(right++)]>=0){
	        count++;
	    }
	    while (count==t.length()){
	        if (minLen>right-left){
	            minLen = right-left;
		    ret = s.substring(left,right);
		}
	        if (++freq[s.charAt(left++)]>0){
	            count--;
		}
	    }
	}
        return ret;
    }

    public static void main(String args[]) {
	System.out.println(new Solution76().minWindow("ADOBECODEBANC","ABC"));
    }

猜你喜欢

转载自blog.csdn.net/qq_24094489/article/details/88430066