c++编程|最小子串覆盖

问题描述 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.
翻译 给定一个字符串s和一个目标字符串t,在字符串s中找到包括所有目标字符串字母的最小长度子串。
思路 1、使用双指针:start指针和end指针,用来表示一个窗口
2、移动end指针,找到符合要求的窗口
3、当发现有效的窗口的时候,移动start指针来找到最小长度的窗口
代码

string minWindow(string s, string t) { unordered_map m; // Statistic for count of char in t for (auto c : t) m[c]++; // counter represents the number of chars of t to be found in s. size_t start = 0, end = 0, counter = t.size(), minStart = 0, minLen = INT_MAX; size_t size = s.size(); // Move end to find a valid window. while (end < size) { // If char in s exists in t, decrease counter if (m[s[end]] > 0) counter--; // Decrease m[s[end]]. If char does not exist in t, m[s[end]] will be negative. m[s[end]]--; end++; // When we found a valid window, move start to find smaller window. while (counter == 0) { if (end - start < minLen) { minStart = start; minLen = end - start; } m[s[start]]++; // When char exists in t, increase counter. if (m[s[start]] > 0) counter++; start++; } } if (minLen != INT_MAX) return s.substr(minStart, minLen); return ""; }

问题延伸 遇到大多数子串问题,给出一个字符串,并且需要找到一个子串,使这个子串满足一些限制。通常方式使用一个哈希表关联两个指针。这个模板如下:
int findSubstring(string s){ unordered_map map; int counter; // check whether the substring is valid int begin=0, end=0; //two pointers, one point to tail and onehead int d; //the length of substringfor() { /* initialize the hash map here */ }while(endif(map[s[end]] ?){/* modify counter here */ } map[s[end]]--; end++; while(/* counter condition */){ /* update d here if finding minimum*///increase begin to make it invalid/valid againif(map[s[begin]] ?){ /*modify counter here*/ } map[s[begin]]++; begin++; }/* update d here if finding maximum*/ } return d; }

1、The code of solving Longest Substring with At Most Two Distinct Characters is below:
int lengthOfLongestSubstringTwoDistinct(string s) { vector map(128, 0); int counter=0, begin=0, end=0, d=0; while(endif(map[s[end++]]++==0) counter++; while(counter>2) if(map[s[begin++]]--==1) counter--; d=max(d, end-begin); } return d; }

【c++编程|最小子串覆盖】2、The code of solving Longest Substring Without Repeating Characters is below:
int lengthOfLongestSubstring(string s) { vector map(128,0); int counter=0, begin=0, end=0, d=0; while(endif(map[s[end++]]++>0) counter++; while(counter>0) if(map[s[begin++]]-->1) counter--; d=max(d, end-begin); //while valid, update d } return d; }

    推荐阅读