Leetcode 76 Minimum Window Substring. (最小窗口子字符串) (滑动窗口, 双指针)

Posted Will

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 76 Minimum Window Substring. (最小窗口子字符串) (滑动窗口, 双指针)相关的知识,希望对你有一定的参考价值。

** Leetcode 76 **

问题描述

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

例子

Example:
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"

解决方案

** Solution Java Method one **
** 3ms, beats 92.14% **
** 39.8MB, beats 46.81% **
class Solution {
    public String minWindow(String s, String t) {
        if (s == null || s.length() == 0 || s.length() < t.length()) 
            return "";
        char[] sArray = s.toCharArray();
        char[] tArray = t.toCharArray();
        int[] map = new int[256];
        for (int i = 0; i < tArray.length; ++i) 
            ++map[tArray[i]];

        int minStart = 0, start = 0;
        int count = tArray.length;
        int minLength = Integer.MAX_VALUE;
        
        for (int end = 0; end < sArray.length; ++end) {
            if (map[sArray[end]] > 0)
                --count;
            --map[sArray[end]];
            while (count == 0) {
                if (end - start + 1 < minLength) {
                    minStart = start;
                    minLength = end - start + 1;
                }
                ++map[sArray[start]];
                if (map[sArray[start]] > 0)
                    ++count;
                ++start;
            }
        }
        if (minStart + minLength > sArray.length) 
            return "";
        return s.substring(minStart, minStart + minLength);
    }
}
** Solution Java Method Two **
** 12ms, 68.18% **
** 39.8MB, 46.81% **
class Solution {
    public String minWindow(String s, String t) {
        if (s == null || s.length() == 0 || s.length() < t.length()) 
            return "";
        Map<Character, Integer> map = new HashMap<>();
        char[] tArray = t.toCharArray();
        char[] sArray = s.toCharArray();
        for(char chr : tArray)
            map.put(chr, map.getOrDefault(chr, 0) + 1);
        
        int count = tArray.length;
        int minStart = 0, minLength = Integer.MAX_VALUE;
        int start = 0;
        
        for(int end = 0; end < sArray.length; ++end){
            char chr = sArray[end];
            if (map.containsKey(chr)) {
                map.put(chr, map.get(chr) - 1);
                if (0 <= map.get(chr))
                    --count;
            }
            while (count == 0){
                if (end - start + 1 < minLength) {
                    minStart = start;
                    minLength = end - start + 1;
                }
                if (map.containsKey(sArray[start])) {
                    map.put(sArray[start], map.get(sArray[start]) + 1);
                    if (map.get(sArray[start]) > 0)
                        ++count;
                }
                ++start;
            }
        }
        if (minStart + minLength > sArray.length) 
            return "";
        return s.substring(minStart, minStart + minLength);
    }
}
** Solution Python3 **
** 96ms, beats 86.89% **
** 13.3MB, beats 61.11% **
class Solution:
    def minWindow(self, s, t):
        need, missing = collections.Counter(t), len(t)
        i = I = J = 0
        for j, c in enumerate(s, 1):
            missing -= need[c] > 0
            need[c] -= 1
            if not missing:
                while need[s[i]] < 0: need[s[i]] += 1; i += 1
                if not J or j - i <= J - I: I, J = i, j
                need[s[i]] += 1; i += 1; missing += 1      
        return s[I : J]

以上是关于Leetcode 76 Minimum Window Substring. (最小窗口子字符串) (滑动窗口, 双指针)的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode76.Minimum Window Substring

LeetCode in Python 76. Minimum Window Substring

leetcode 76. Minimum Window Substring

Leetcode 76: Minimum Window Substring

leetcode 76-Minimum Window Substring(hard)

leetcode76 Minimum Window Substring