Skip to content

Commit

Permalink
Create cpp program for minimum window substring
Browse files Browse the repository at this point in the history
tanus786#773 please assign me this issue . like to contribute the optimized and efficient code in this repo.
  • Loading branch information
shirali-saraf authored Oct 6, 2023
1 parent 067b21a commit ffe61e8
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cpp program for minimum window substring
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
string minWindow(string s, string t) {
if(s.size() < t.size()){
return "";
}
unordered_map<char,int> map;
for(int i=0;i<t.size();i++){
map[t[i]]++;
}
int count=0,start=0,min_length = INT_MAX, min_start = 0;
for(int end=0; end<s.size(); end++){
if(map[s[end]]>0){
count++;
}
map[s[end]]--;
if(count == t.length()) {
while(start < end && map[s[start]] < 0){
map[s[start]]++, start++;
}
if(min_length > end-start){
min_length = end-(min_start=start)+1;
}
map[s[start++]]++;
count--;
}
}
return min_length == INT_MAX ? "" : s.substr(min_start, min_length);
}

0 comments on commit ffe61e8

Please sign in to comment.