-
Notifications
You must be signed in to change notification settings - Fork 0
/
minwindowsubstr.cpp
76 lines (58 loc) · 2.06 KB
/
minwindowsubstr.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class Solution {
public:
string minWindow(string S, string T) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int mark[500];
int acc[500];
int count,left=0,right;
string result = "";
memset(mark,0,sizeof(mark));
memset(acc,0,sizeof(acc));
count = T.size();
for(int i = 0; i < T.size(); i ++){
mark[T[i]] ++;
acc[T[i]] ++;
}
for(int i = 0; i < S.size(); i ++){
if(acc[S[i]] == 0){
continue;
}
else{
if(mark[S[i]] > 0){
count--;
}
mark[S[i]] --;
if(count == 0){
while(left < S.size()){
if(acc[S[left]] == 0){
left++;
}
else{
if(mark[S[left]] >= 0)
break;
else if(mark[S[left]] < 0){
mark[S[left]]++;
left++;
}
}
}
int len = i-left+1;
if(result.size() == 0 || len < result.size()){
result = S.substr(left,len);
}
while(left<= i){
if(acc[S[left]] > 0){
mark[S[left]] ++;
count ++;
left++;
break;
}
left++;
}
}
}
}
return result;
}
};