zoukankan      html  css  js  c++  java
  • Leetcode: 76. Minimum Window Substring

    Description

    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.

    思路

    • 刚开始自己用unordered_map写了一个出来,159ms...
    • 往上找了一个,用两个hash数组做的,确实比我叼。

    代码

    • unordered_map
    class Solution {
    public:
     string minWindow(string s, string t) {
    		int slen = s.size(), tlen = t.size();
    		if (slen == 0 || tlen == 0) return "";
    
    		string res = "";
    		int start = 0, end = -1;
    		int i = 0;
    		unordered_map<char, int> tMap;
    		unordered_map<char, int> cMap;
    		unordered_map<char, int> helpMap;
    
    		for (i = 0; i < tlen; ++i)
    			tMap[t[i]]++;
    
    		i = 0;
    		while (i < slen){
    			if (tMap.find(s[i]) == tMap.end())
    				i++;
    			else break;
    		}
    
    		start = i;
    
    		while (i < slen){
    			if (tMap.count(s[i]) > 0){
    				helpMap[s[i]]++;
    				if (tMap[s[i]] > cMap[s[i]])
    					cMap[s[i]]++;
    			}
    
    			if (tMap == cMap){
    				do{
    					if (res == "" || res.size() > (i - start + 1)){
    						res = s.substr(start, i - start + 1);
    					}
    
    					if (cMap[s[start]] > 1)
    						cMap[s[start]]--;
    					else cMap.erase(s[start]);
    
    					if (helpMap[s[start]] > 1)
    						helpMap[s[start]]--;
    					else helpMap.erase(s[start]);
    
    					if (helpMap[s[start]] > cMap[s[start]])
    						cMap[s[start]]++;
    
    					int j = start + 1;
    					while (j <= i){
    						if (tMap.find(s[j]) == tMap.end())
    							j++;
    						else break;
    					}
    
    					start = j;
    				} while (cMap == tMap);
    			}
    			
    			i++;
    		}
    
    		if (tMap == cMap && (res == "" || res.size() > (slen - start)))
    				res = s.substr(start, i - start + 1);
    		return res;
    	}
    };
    
    • hash数组
    class Solution {
    public:
       string minWindow(string s, string t) 
    	{
    		string res;
    		if (s.size() < t.size()) return res;
    	    vector<int> flag(256, 0);
    	    vector<int> hash(256, 0);
    	    for(int i = 0; i < t.size(); ++i){
    	        flag[t[i]]++;
    	        hash[t[i]]++;
    	    }
    	   
    	    int count = 0;
    	    int start = 0, minsize = s.size() + 1, minflag = -1;
    	    for(int i = 0; i < s.size(); ++i){
    	        if(flag[s[i]] > 0){
    	            if(--hash[s[i]] >= 0)  //这个地方
    	               count++;
    	               
    	           while(count == t.size()){
    	                if(i - start + 1 < minsize){
    	                   minsize = i - start + 1;
    	                   minflag = start;
    	               }
    	               
    	               //这个地方,思路确实巧妙,不得不服
    	               if(flag[s[start]] > 0){
    	                   if(++hash[s[start]] > 0)
    	                        --count;
    	               }
    	               ++start;
    	           }
    	        }
    	        
    	    }
    	    
    	    if(minsize > s.size()) return "";
    	    return s.substr(minflag, minsize);
    	}
    };
    
  • 相关阅读:
    POJ 1426 Find The Multiple(数论——中国同余定理)
    POJ 2253 Frogger(Dijkstra变形——最短路径最大权值)
    POJ 3790 最短路径问题(Dijkstra变形——最短路径双重最小权值)
    POJ 3278 Catch That Cow(模板——BFS)
    HDU 1071 The area
    HDU 1213 How Many Tables(模板——并查集)
    POJ 1611 The Suspects
    light oj 1214 Large Division
    POJ 1258 Agri-Net(Prim算法求解MST)
    POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)
  • 原文地址:https://www.cnblogs.com/lengender-12/p/6927167.html
Copyright © 2011-2022 走看看