LeetCode "Minimum Window Substring" - 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 procedure.

class Solution {
public:
    string minWindow(string S, string T) {
        if (T.length() > S.length()) return "";

        //    Build Dict: Yes chars in T could be duplicated
        unordered_map<char, int> dict;
        for (int i = 0; i < T.length(); i++)
        {
            if (dict.find(T[i]) == dict.end())        dict.insert(make_pair(T[i], 1));
            else                                    dict[T[i]] ++;
        }

        //    Build Indices - char - cnt
        size_t ttlCnt = 0;
        int iLeft = 0;
        int minL = 0, minLen = std::numeric_limits<int>::max();
        unordered_map<char, size_t> rec;
        for (int i = 0; i < S.length(); i++)
        {
            char c = S[i];
            if (dict.find(c) != dict.end())
            {
                if (rec.find(c) == rec.end())    rec[c] = 1;
                else                            rec[c] ++;

                if (rec[c] == dict[c]) ttlCnt++;
                if (ttlCnt == dict.size())
                {
                    //    Shrink from Left
                    while ( dict.find(S[iLeft]) == dict.end() ||    // irrelavant char?
                            (iLeft < i && rec[S[iLeft]] > dict[S[iLeft]])) // redundant char?
                    {
                        if (dict.find(S[iLeft]) != dict.end())
                            rec[S[iLeft]] --;
                        iLeft++;
                    }
                    //    Update record
                    if ((i - iLeft + 1) < minLen)
                    {
                        minL = iLeft;
                        minLen = (i - iLeft + 1);
                    }
                }
            }
        }
        if (minLen == std::numeric_limits<int>::max()) return "";
        return S.substr(minL, minLen);
    }
};

LeetCode "Minimum Window Substring" - STAR

时间: 2024-11-05 13:31:04

LeetCode "Minimum Window Substring" - STAR的相关文章

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 @ 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] 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

[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

Leetcode Minimum Window Substring

题目地址:https://leetcode.com/problems/minimum-window-substring/ 题目解答: import java.util.HashMap; import java.util.Map; public class Solution { public String minWindow(String S, String T) { if(S == null || S.length() == 0 || T == null || T.equals("")

【leetcode】Minimum Window Substring

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

【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

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: