76. Minimum Window Substring
problem description
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"algorithm thought
code
class Solution {
public:
string minWindow(string s, string t) {
vector<int> map(128,0);
for(auto c:t)
map[c]++;
int begin=0,end=0,head=0,length=INT_MAX;
int count=t.size();
while(end<s.size()){
if(map[s[end++]]-->0) count--;
while(count==0){
if(length>end-begin) length=end-(head=begin);
if(map[s[begin++]]++>=0) count++;
}
}
return length==INT_MAX?"":s.substr(head,length);
}
};algorithm analysis
Last updated