Skip to content

Commit

Permalink
Merge pull request #280 from anmolbtw/master
Browse files Browse the repository at this point in the history
Create minSubstringWindow.cpp
  • Loading branch information
SR-Sunny-Raj authored Oct 30, 2022
2 parents 33d4978 + 3a4a24a commit 6edcaf2
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Coders/DSA Concepts/minSubstringWindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
public:
string minWindow(string s, string t) {
string res = "";
if(s.size() < t.size()) return res;
unordered_map<char, int> umap;
for(auto it: t) umap[it]++;
int i=0, j=0;
int length = INT_MAX, count = umap.size();
while(j<s.size()){
if(umap.find(s[j])!=umap.end()){
umap[s[j]]--;
if(umap[s[j]]==0) count--;
}
if(count > 0) j++;
else if(count == 0){
while(count==0){
if(umap.find(s[i])!=umap.end()){
if(j-i+1 < length){
length = j-i+1;
res = s.substr(i, j-i+1);
}
umap[s[i]]++;
if(umap[s[i]] > 0) count++;
}
i++;
}
j++;
}
}
return res;
}
};

0 comments on commit 6edcaf2

Please sign in to comment.