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".
S: 窗口型two poniter
用hashmap记录target中元素出现次数,用count记录总的target元素出现情况
string minWindow(string &source, string &target) {
unordered_map<char, int> map;
int count = t.size();
string result = "";
if (count == 0) return result;
for (char i : t) {
map[i]++;
}
for (int i = 0, j = 0; j < s.size(); ++j) {
if (map.find(s[j]) != map.end()) {
if (map[s[j]] > 0) {
count--;
}
map[s[j]]--;
}
while (count == 0) {
if (result.size() == 0 || result.size() > j - i + 1) {
result = s.substr(i, j - i + 1);
}
if (map.find(s[i]) != map.end()) {
if (map[s[i]] == 0) {
count++;
}
map[s[i]]++;
}
i++;
}
}
return result;
}