LeetCode[Hash Table]: 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 in S that covers all characters in T, return the emtpy string "".

If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

对于这个题目,我毫无头绪,当我在Discuss上看懂这个解法的时候:https://oj.leetcode.com/discuss/10337/accepted-o-n-solution我觉得这个解法实在是太妙了!这个链接有关于这个解法的idea:https://oj.leetcode.com/discuss/5469/is-the-length-of-t-considered-constant-or-m

下面先贴出这个解法的代码:

    string minWindow(string S, string T) {
        int count = T.size();

        int  require[128] = { 0 };
        bool charSet[128] = { false };
        for (int n = 0; n < count; ++n) {
            require[T[n]]++;
            charSet[T[n]] = true;
        }

        int i = -1, j = 0;
        int minLen = INT_MAX, minIdx = 0;
        while (i < (int)S.size() && j < (int)S.size()) {
            if (count) {
                i++;
                require[S[i]]--;
                if (charSet[S[i]] && require[S[i]] >= 0) count--;
            }
            else {
                if (minLen > i - j + 1) {
                    minLen = i - j + 1;
                    minIdx = j;
                }
                require[S[j]]++;
                if (charSet[S[j]] && require[S[j]] > 0) count++;
                j++;
            }
        }

        return (minLen == INT_MAX ? "" : S.substr(minIdx, minLen));
    }

直观地理解这个算法是比较困难的,建议用一些例子跑起来,这样会比较容易理解这个算法为什么能到期望的目的。总之我很为这个解法而惊叹!

这个算法的时间性能表现如下图所示:

时间: 2024-12-13 15:42:19

LeetCode[Hash Table]: Minimum Window Substring的相关文章

【leetcode】Minimum Window Substring

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

Minimum Window Substring leetcode

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:

LeetCode: Minimum Window Substring [076]

[题目] 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

【leetcode刷题笔记】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

Minimum Window Substring leetcode 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 w

[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

LeetCode &quot;Minimum Window Substring&quot; - STAR

Main reference: http://zhaohongze.com/wordpress/2014/01/04/leetcode-minimum-window-substring/ The ART of counting. So difficult and beautiful problem. It is all about dynamic window maintanence. Essentially, it is still a fancy linear scanning proced

leetcode第一刷_Minimum Window Substring

好题,字符串,线性时间. 我觉得第一次拿到这个题的人应该不会知道该怎么做吧,要么就是我太弱了..先搞清楚这个题要求的是什么.从一个长字符串中找一个字串,这个字串中的字符完全包含了另一个给定目标串中的字符,且这个字串的长度要求最小.还有一个非常重要的简化,题干指明了要求这种最短字串只有一个,这个限制其实暗示了这道题的整体思路,只要找到一个长串,然后缩减到不能缩减即可. 从题目的要求出发可以发现,这道题对于字符串中字符的顺序是没有要求的,因此可以很自然的想到用hash表来保存目标串中的每个字符的个数

53. Minimum Window Substring

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: