-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum_Window_Substring.cpp
More file actions
61 lines (53 loc) · 1.64 KB
/
Minimum_Window_Substring.cpp
File metadata and controls
61 lines (53 loc) · 1.64 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
checked anson's ,mine is better
needtofind->in
hasfound->counts
*/
//O(M+N)
class Solution {
public:
string minWindow(string S, string T) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int M=S.size(),N=T.size();
string res="";
queue<int> queue;
if(M<N)return res;
int counts[128]={0},sum=N,min=M+1;
bool in[128]={false};
for(int i=0;i<N;i++)
{
counts[T[i]]++;
in[T[i]]=true;
}
for(int i=0;i<M;i++)
{
if(in[S[i]])
{
queue.push(i);
counts[S[i]]--;
if(counts[S[i]]>=0)sum--;
if(sum==0)
{
int start=queue.front();
int end=queue.back();
while(counts[S[start]]<0)
{
counts[S[start]]++;
queue.pop();
start=queue.front();
}
if(end-start+1<min)
{
min=end-start+1;
res=S.substr(start,end-start+1);
}
counts[S[start]]++;
queue.pop();
sum++;
}
}
}
return res;
}
};