forked from tanus786/CP-Codes-HackOctober-Fest-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create cpp program for minimum window substring
tanus786#773 please assign me this issue . like to contribute the optimized and efficient code in this repo.
- Loading branch information
1 parent
067b21a
commit ffe61e8
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |