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]

原文地址:https://www.cnblogs.com/willwuss/p/12276083.html

时间: 2024-07-31 05:36:45

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

leetCode 76.Minimum Window Substring(最小窗口子串) 解题思路和方法

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). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". Note: If there is no such windo

leetcode[76] Minimum Window Substring

给定两个串,S和T,在S中找到包含T的最短子串,如果不能包含,返回空字符. 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). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC&

leetcode 76 Minimum Window Substring ----- java

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). For example,S = "ADOBECODEBANC"T = "ABC" Minimum window is "BANC". Note:If there is no such window i

【leetcode】Minimum Window Substring

问题: 给定两个字符串,S,T,返回S中包含T中所有字符的最短的字串,若不存在,则返回"".时间复杂度为O(n). 例如:S = "ADOBCODEBANC" T = "ABC" 返回BANC 生活场景: 把问题具体化现实化一点.有n层楼,每层楼里放有一个物品,现在老板给你一个物品清单,里面是要你集齐的物品,你可以乘坐电梯,但是电梯只停一次,停在哪一层,就从哪一层开始向楼上搜集物品,至于要在那层停电梯,由你自己选择. 这里我们当然选择即能集齐物品

LeetCode in Python 76. Minimum Window Substring

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"Note: If there is no such window in S th

Java for LeetCode 076 Minimum Window Substring

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). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". Note: If there is no such windo

[Leetcode][JAVA] Minimum Window Substring

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). For example,S = "ADOBECODEBANC"T = "ABC" Minimum window is "BANC". Note:If there is no such window i

76. Minimum Window Substring

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). For example,S = "ADOBECODEBANC"T = "ABC" Minimum window is "BANC". Note:If there is no such window i

[leedcode 76] Minimum Window Substring

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). For example,S = "ADOBECODEBANC"T = "ABC" Minimum window is "BANC". Note:If there is no such window i