zoukankan      html  css  js  c++  java
  • 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.

    思路:这道题最小覆盖子串,意思就是说在S中找到能全部覆盖T中元素的最小子串。使用两个指针begin和end,以及两个hashtable(hastable1和hastable2),hastable1表示T中元素的个数,hastable2表示当前收集到的T中元素的个数;count表示当前收集到的字母总数,当然这个字母是T中元素。当count等于T的长度时,我们就找到了一个满足条件的window。用指针end来遍历S,当遇到一字母ch,且ch是T中的字母,则hastable2[ch]加1,如果hastable2[ch]<=hastable1[ch],则count也要增加1.当count为T长度时,我们递增begin,同时保证count为T长度。

    这里面有个很重要的问题,就是如何保证count始终为T的长度?假设begin指向字母ch,如果hastable2[ch]>hastable1[ch],则hastable2[ch]--,同时begin++,去除冗余的字母。这样一个合法的window出现,记录开始和结尾指针,同时更新最小长度。

    class Solution {
    public:
        string minWindow(string S, string T) {
            int sLen=S.size();
            int tLen=T.size();
            if(sLen<=0||tLen<=0)
                return "";
            int minBeg,minEnd;
            int minWindow=sLen+1;
            int count=0;
            int hastable1[256]={0},hastable2[256]={0};
            for(int i=0;i<tLen;i++)
            {
                hastable1[T[i]]++;
            }
            for(int begin=0,end=0;end<sLen;end++)
            {
                if(hastable1[S[end]]==0)
                    continue;
                char ch=S[end];
                hastable2[ch]++;
                if(hastable2[ch]<=hastable1[ch])
                    count++;
                if(count==tLen)
                {
                    while(hastable1[S[begin]]==0||hastable1[S[begin]]<hastable2[S[begin]])
                    {
                        if(hastable1[S[begin]]<hastable2[S[begin]])
                            hastable2[S[begin]]--;
                        begin++;
                    }
                    int length=end-begin+1;
                    if(length<minWindow)
                    {
                        minBeg=begin;
                        minEnd=end;
                        minWindow=length;
                    }
                }
            }
            return minWindow<=sLen?S.substr(minBeg,minWindow):"";
        }
    };

     参考:最小覆盖子串

  • 相关阅读:
    JS获取本周、本季度、本月、上月的开始日期、结束日期
    《大教堂和集市》笔记——为什么一个本科生业余作品却成了全世界最流行的操作系统之一Linux?
    “平头哥”半导体公司
    阻挡不住的变化
    给我五分钟,带你走出迷茫
    涂子沛:从幼稚到成熟,我们这个时代的数据革命
    统计思维就是透过现象看本质
    十个有趣的“大数据”挖掘案例
    信息时代与大数据相关的8个专业
    大数据招聘分析
  • 原文地址:https://www.cnblogs.com/awy-blog/p/3822073.html
Copyright © 2011-2022 走看看