Skip to content

Commit e38184b

Browse files
committed
shortest substring containing all the characters in the set-Square
1 parent 11ad9c2 commit e38184b

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Solution/Day-103.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
class Solution{
4+
public:
5+
string minWindow(const string &s , const string &pattern){
6+
unordered_map<char , int>st,pt;
7+
for(auto&itr:pattern){
8+
pt[itr]++;
9+
}
10+
int start = 0 , end_ = s.size(), i=0;
11+
int min_window =INT_MAX;
12+
string ans;
13+
int win_size=0;
14+
list<char>q;
15+
while(i<end_){
16+
st[s[i]]++;
17+
q.push_back(s[i]);
18+
if(pt.find(s[i])!=pt.end()){
19+
win_size++;
20+
}
21+
if(win_size >= (int)pattern.size()){
22+
while(start < i && (pt.find(s[start])==pt.end() || st[s[start]]-1>=pt[s[start]])){
23+
st[s[start]]--;
24+
q.pop_front();
25+
++start;
26+
}
27+
string temp(q.begin() , q.end());
28+
if(ans.size()==0 || temp < ans){
29+
ans = temp;
30+
}
31+
min_window = min(min_window , i - start +1);
32+
}
33+
++i;
34+
}
35+
return ans;
36+
}
37+
};
38+
int main(void){
39+
Solution s1;
40+
cout << s1.minWindow("figehaeci","aei") << '\n';
41+
return 0;
42+
}

0 commit comments

Comments
 (0)