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 in S that covers all characters in T, return the empty string "".

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

思路:问题可以转变为,T中各个字符的数量<= window中的这些字符的数量,所以用map纪录字符与字符数量的关系

class Solution {
public:
    bool ifContain(map<char,int>& source, map<char,int>& target) //check if S contains T
    {
        for(map<char,int>::iterator it = target.begin(); it != target.end(); it++) //map的遍历
        {
            if(source[it->first] < it->second) return false;
        }
        return true;
    }

    string minWindow(string S, string T) {
        int minLength = INT_MAX;
        int start = 0, end = 0, minStart = 0, minEnd = 0;
        map<char,int> source;
        map<char,int> target;
        source[S[start]]++; //map的插入[法I]source[key]=value; [法II]source.insert(make_pair(key,value));
        for(int i = 0; i< T.length(); i++)
        {
            target[T[i]]++;
        }
        while(1)
        {
            if(ifContain(source, target)){
                if(end-start+1 < minLength)
                {
                    minStart = start;
                    minEnd = end;
                    minLength = end-start+1;
                    if(minLength == T.size()) return S.substr(minStart,minLength);
                }
                source[S[start]]--; //寻找更小的窗口
                start++;
            }
            else //不包含,则扩大窗口
            {
                end++;
                if(end==S.size()) break;
                source[S[end]]++;
            }
        }
        if(minLength>S.size()) return "";
        else return S.substr(minStart,minLength);
    }
};
时间: 2024-11-03 19:48:34

76. Minimum Window Substring (Map)的相关文章

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

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

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

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