leetcode 76. Minimum Window Substring

來源:互聯網
上載者:User

標籤:exit   思路   差距   lin   注意   div   i++   ado   concat   

link

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.

 

題意:

設T裡的字元i有aim[i]個,在S中求最短子串X, 使得X中的字元i有now[i]個且滿足對任意i有now[i] >= aim[i]。 保證最短的子串只有一個。

 

思路:

和之前那個concate一個思路,其實也是維護一段視窗內的資訊。

變化的地方只在於,裡面有多餘的字元也沒有關係。

 

因此對於右端,一個新加入的字元s[i], 首先肯定是要加進去的。

如果s[i]出現在aim中,則其的加入可能可以使得now[i] > aim[i], 也就是我們可以從左端消除一些字元。 因此用一個while條件判斷左端是否滿足now[s[left]] > aim[s[left]]的條件, 滿足的話刪去左端點是沒有問題的。

在實現的時候偷了個懶,用aim直接計算目前視窗中的數量和目標數量的差距,也就是aim = aim - now。並且只維護起始的aim不為零的部分。那麼while的條件就變為了aim[s[left]] < 0

在統計是否所有的i都成立的時候,注意我們加入一個i的時候有三種情況。

1. i再加一個就滿足了aim[i] <= now[i]的情況。也就是說此時now[i} = aim[i] - 1, 用我們的統計方法也就是aim[i] = 1, 加入後aim[i]恰好變為0

2. i加入多於目標個了,也就是now[i] >= aim[i] , (aim[i] <= 0) 加入 後消除aim等號,變為aim[i] < 0

3 加入i也不夠aim[i], 也就是aim[i] > 0

因此只有在加入i後aim[i]變為0,才能使一個新的i成立。 統計aim中非零的數量,在aim[i] = 0時更新計數器,直至所有i成立,為第一個可行解。當第一個可行解出現後,用我們更新視窗的方法後面的所有情況都是可行的。

code:

class Solution {public:    string minWindow(string s, string t) {        string ans = "";        int nowMinLength = INT_MAX;                unordered_map<char, int> aim;        int charCount = 0;        for(int i = 0; i < t.length(); i++){            if(aim.count(t[i]) == 0) charCount ++;            aim[t[i]] ++;        }        int left = 0, cnt = 0;        for(int i = 0; i < s.length(); i++){            //if(aim.count(s[i]) == 1){                aim[s[i]] --;                if(aim[s[i]] == 0) cnt ++;                while(aim[s[left]] < 0 &&  left < i){                    aim[s[left]] ++;                    left ++;                }                if(cnt == charCount){                    int tmpLen = i - left + 1;                    if(tmpLen < nowMinLength){                        nowMinLength = tmpLen;                        ans = s.substr(left, tmpLen);                    }                }            //}                    }        return ans;    }};

 

leetcode 76. Minimum Window Substring

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.