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 that covers all characters in T, return the empty string "".
If there is such window, you are guaranteed that there will always be only one unique minimum window in S.

滑动窗口算法可解,用vector代替哈希表获得更快的速度,遍历s串时相应增加cnt,找到符合要求的window后开始缩小左边界,cnt不符合要求时扩大右边界。关键点是要先找到符合要求的window。

class Solution {
public:
    string minWindow(string s, string t) {
        vector<int> stats(128, 0);
        int cnt = 0, left = 0, minLen = INT_MAX;
        string res = "";
        for (char c : t) stats[c]++;
        for (int i = 0; i < s.size(); i++) {
            if (--stats[s[i]] >= 0) cnt++;

            while (cnt == t.size()) {
                //找到符合要求的window后再缩小左边界
                if (i - left + 1 < minLen) {
                    minLen = i - left + 1;
                    res = s.substr(left, minLen);
                }
                if (++stats[s[left]] > 0) cnt--;
                left++;
            }
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/lowkeysingsing/p/11282130.html

时间: 2024-08-28 04:08:18

LeetCode in Python 76. Minimum Window Substring的相关文章

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&qu

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

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

76. Minimum Window Substring (Map)

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

[LC] 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

[leetcode]Minimum Window Substring @ Python

原题地址:https://oj.leetcode.com/problems/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" M