#32 Minimum Window Substring

本文介绍了一种寻找字符串中包含目标字符集的最小子串的高效算法,并通过双指针技巧实现时间复杂度为O(n)的解决方案。示例中以ADOBECODEBANC为目标源字符串,ABC为目标字符集,展示了如何找到最小覆盖子串BANC。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值