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

    class Solution {
    public:
    /*
    string minWindow(string S, string T) 
    {
        string res="";
        if(S.empty()||T.empty()||S.size()<T.size())return res;
        int countNeed[256]={0};
        int countHas[256]={0};
        int countT=T.size();
        for (int i=0;i<countT;i++)
        {
            countNeed[T[i]]++;
            countHas[T[i]]++;
        }
        int start=0;
        int minSize=INT_MAX;
        for (int end=0;end<S.size();end++)
        {
            if (countHas[S[end]]>0)
            {
                countNeed[S[end]]--;
                if(countNeed[S[end]]>=0)countT--;
            }
            if (countT==0)
            {
                while(start<S.size())
                {
                    if (countHas[S[start]]>0)
                    {
                        if (countNeed[S[start]]<0)
                        {
                            countNeed[S[start]]++;
                            start++;
                        }
                        else  break;
                    }
                    else  start++;
                }
                if (end-start+1<=minSize)
                {
                    minSize=end-start+1;
                    res=S.substr(start,minSize);
                }
            }
        }
        return res;
    }
    */
    
    string minWindow(string S, string T) 
    {
        string res="";
        if(S.empty()||T.empty()||S.size()<T.size())return res;
        int countT=T.size();
        map<char,int> MapNeed;
        for (int i=0;i<countT;i++)
        {
            MapNeed[T[i]]=0;
        }
        for (int i=0;i<countT;i++)
        {
            MapNeed[T[i]]++;
        }
        int start=0;
        int minSize=INT_MAX;
        for (int end=0;end<S.size();end++)
        {
            if (MapNeed.find(S[end])!=MapNeed.end())
            {
                MapNeed[S[end]]--;
                if (MapNeed[S[end]]>=0)countT--;
            }
            if (countT==0)
            {
                while (start<S.size())
                {
                    if (MapNeed.find(S[start])!=MapNeed.end())
                    {
                        if (MapNeed[S[start]]<0)
                        {
                            MapNeed[S[start]]++;
                            start++;
                        } 
                        else
                        {
                            break;
                        }
                    }
                    else  
                    {
                        start++;
                    }
                }
                if (end-start+1<=minSize)
                {
                    minSize=end-start+1;
                    res=S.substr(start, minSize);
                }
            }
        }
        return res;
    }
    
    };
  • 相关阅读:
    阿里轻量应用服务器 Tomcat 注意的地方 Unsupported major.minor version 52.0(unable to load class
    微信小程序-进度条循环播放
    微信小程序-动画
    微信小程序-自定义分享
    微信小程序-引导页
    微信小程序-时间轴
    微信小程序-两次点击不同图片交换图片位置
    Kafka消费者——消费者客户端多线程实现
    Kakfa消费者——原理及分析
    Kafka基础——Kafka架构
  • 原文地址:https://www.cnblogs.com/Vae1990Silence/p/4281461.html
Copyright © 2011-2022 走看看