forked from elixered/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path76-minimum-window-substring.cpp
43 lines (43 loc) · 1.01 KB
/
76-minimum-window-substring.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
public:
string minWindow(string s, string t) {
int m = s.size();
int n = t.size();
unordered_map<int,int> hm;
for(auto& it:t)
hm[it]++;
int count = hm.size();
int i=0,j=0;
string ans = "";
int idx = -1, mini = m;
while(j<m)
{
char a = s[j];
if(hm.find(a)!=hm.end())
{
hm[a]--;
if(hm[a]==0)
count--;
}
while(count==0 && i<=j)
{
if(j-i+1<=mini)
{
idx = i;
mini = j-i+1;
}
char b = s[i];
if(hm.find(b)!=hm.end())
{
hm[b]++;
if(hm[b]>0)
count++;
}
i++;
}
j++;
}
if(idx==-1) return "";
return s.substr(idx,mini);
}
};