#32 Minimum Window Substring

原创 2016年08月29日 11:06:25

题目描述:

Given a string source and a string target, find the minimum window in source which will contain all the characters in target.

 Notice

If there is no such window in source that covers all characters in target, return the emtpy string "".

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

Clarification

Should the characters in minimum window has the same order in target?

  • Not necessary.
Example

For source = "ADOBECODEBANC", target = "ABC", the minimum window is "BANC"

Challenge 

Can you do it in time complexity O(n) ?

题目思路:

这题还是用two pointers的思想。如果得到的substring已经满足要求了,我们就让l前进来shrink这个substring;如果还没满足要求,那就让r前进来加入新的char。满足要求的意思,就是建的hashmap中,所有的value都小于等于0.

Mycode(AC = 28ms):

class Solution {
public:    
    /**
     * @param source: A string
     * @param target: A string
     * @return: A string denote the minimum window
     *          Return "" if there is no such a string
     */
    string minWindow(string &source, string &target) {
        // write your code here
        if (source == "" || target == "") {
            return "";
        }
        
        // put the chars in target into map
        map<char, int> counter;
        source = source + " ";
        for (int i = 0; i < target.size(); i++) {
            if (counter.find(target[i]) != counter.end()) {
                counter[target[i]] += 1;
            }
            else {
                counter[target[i]] = 1;
            }
        }
        
        int l = 0, r = 0;
        string ans = "";
        
        // use two pointers to find the minimum length substring
        while (r < source.size()) {
            if (isValid(counter)) {
                string tmp = source.substr(l, r - l);
                if (ans == "" || tmp.size() < ans.size()) {
                    ans = tmp;
                }
                adjustCounter(counter, source[l], true);
                l++;
            }
            else {
                adjustCounter(counter, source[r], false);
                r++;
            }
        }
        
        return ans;
    }
    
    // add or delete a char in the counter
    void adjustCounter(map<char, int>& counter, char key, bool add) {
        if (counter.find(key) != counter.end()) {
            counter[key] = add? counter[key] + 1: counter[key] - 1;
        }
    }
    
    // if all chars in target (is valid), then all keys' value in counter should
    // be <= 0
    bool isValid(map<char, int>& counter) {
        for (auto it = counter.begin(); it != counter.end(); it++) {
            if (it->second > 0) return false;
        }
        return true;
    }
};


版权声明:三千微尘里,吾宁爱与憎

相关文章推荐

32.Minimum Window Substring-最小子串覆盖(中等题)

最小子串覆盖 题目给定一个字符串source和一个目标字符串target,在字符串source中找到包括所有目标字符串字母的子串。注意事项 如果在source中没有这样的子串,返回”“,如果有多个...

hrbust 1758 Minimum Window Substring【尺取法】好题

Minimum Window Substring Time Limit: 1000 MS Memory Limit: 65535 K Total Submit: 51(17 u...

LeetCode Minimum Window Substring

Description: Given a string S and a string T, find the minimum window in S which will contain a...

Leetcode 76. Minimum Window Substring 最小子串窗口 解题报告

1 解题思想这题是说在S串当中,找出一个最小的窗口,这个窗口正好包含了T中的所有字符。其中在S当中只要包含了T的所有字符(包括出现的次数也要满足)就可以了,可以有多余的字符,顺序也不限制。基本思想就是...

LeetCode | Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will contain all the characters ...

LeetCode | Minimum Window Substring

题目: Given a string S and a string T, find the minimum window in S which will contain all the ch...

[LeetCode]76.Minimum Window Substring

题目Given a string S and a string T, find the minimum window in S which will contain all the character...

76. Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will contain all the characters ...

LeetCode —— Minimum Window Substring

链接:http://leetcode.com/onlinejudge#question_76 原题: Given a string S and a string T, find the min...

LeetCode Minimum Window Substring

题目
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)