• 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 empty string "".

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

       public string MinWindow(string s, string t) {
            if(t==null || t == "" || s == "") return t;
            if(t.Length > s.Length) return "";
            int max =s.Length+1;
            var res ="";
            var hashtable = new Dictionary<char,int>();
            int min =s.Length;
            for(int i =0;i< t.Length;i++)
            {
                if(hashtable.ContainsKey(t[i])) hashtable[t[i]]++;
                else hashtable.Add(t[i],1);
            }
            
            for(int i = 0;i<=s.Length - t.Length;i++)
            {
                var exist = new Dictionary<char,int>();
                int count =0;
                for(int j = i;j<s.Length;j++)
                {
                    if(max < j-i+1) break;
                    if(hashtable.ContainsKey(s[j]))
                    {
                        if(exist.ContainsKey(s[j]))
                        {
                            if(exist[s[j]] < hashtable[s[j]])
                            {
                                count++;
                                exist[s[j]]++;
                                if(count >= t.Length)
                                {
                                    if(max > j-i+1) {res =s.Substring(i,j-i+1);max = j-i+1;}
                                    break;
                                }
                            }
                        }
                        else
                        {
                            count++;
                            exist.Add(s[j],1);
                            if(count >= t.Length) 
                            {
                                if(max > j-i+1) {res =s.Substring(i,j-i+1);max = j-i+1;}
                                    break;
                            }
                        }
                    }
                }
            }
            return res;
        }
    public string MinWindow(string s, string t) {
            if(t==null || t == "" ||s==mull|| s == "") return t;
            if(t.Length > s.Length) return "";
            int max =s.Length+1;
            var res ="";
            var hashtable = new Dictionary<char,int>();
            for(int i =0;i< t.Length;i++)
            {
                if(hashtable.ContainsKey(t[i])) hashtable[t[i]]++;
                else hashtable.Add(t[i],1);
            }
            
            int left =0;
            int right =-1;
            int count = 0;
            while(right<s.Length)
            {
                if(count == t.Length)
                {
                    if(max > right - left + 1)
                    {
                        max= right - left + 1;
                        res = s.Substring(left,right - left + 1);
                    }
                    if(hashtable.ContainsKey(s[left]))
                    {
                        if(hashtable[s[left]] ==0)  count--;
                        hashtable[s[left]]++;
                       
                    }
                   
                    left++;
                }
                else if(right<s.Length-1)
                {
                    right++;
                    if(hashtable.ContainsKey(s[right]) )
                    {
                        if(hashtable[s[right]]>0) count++;
                        hashtable[s[right]]--;
                    }
                }
                else right++;
                
            }
            return res;
    
        }
  • 相关阅读:
    UVA 11925 Generating Permutations 生成排列 (序列)
    UVA 1611 Crane 起重机 (子问题)
    UVA 11572 Unique snowflakes (滑窗)
    UVA 177 PaperFolding 折纸痕 (分形,递归)
    UVA 11491 Erasing and Winning 奖品的价值 (贪心)
    UVA1610 PartyGame 聚会游戏(细节题)
    UVA 1149 Bin Packing 装箱(贪心)
    topcpder SRM 664 div2 A,B,C BearCheats , BearPlays equalPiles , BearSorts (映射)
    UVA 1442 Cave 洞穴 (贪心+扫描)
    UVA 1609 Foul Play 不公平竞赛 (构(luan)造(gao)+递归)
  • 原文地址:https://www.cnblogs.com/renyualbert/p/5911355.html
Copyright © 2020-2023  润新知