-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumWindowSubstring.cpp
More file actions
48 lines (45 loc) · 934 Bytes
/
Copy pathMinimumWindowSubstring.cpp
File metadata and controls
48 lines (45 loc) · 934 Bytes
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
#include<bits/stdc++.h>
using namespace std;
int main() {
string s, t, result = "";
int answer = INT_MAX;
cin >> s >> t;
vector<int> freqT(26, 0);
vector<int> freqS(26, 0);
for(char a : t) {
freqT[a - 'a']++;
}
int start = 0, end = 0, count = 0;
while(end < s.length()) {
if(count == t.length()) {
if(answer > end - start) {
answer = end - start;
result = s.substr(start, end - start);
}
freqS[s[start] - 'a']--;
if(freqS[s[start] - 'a'] < freqT[s[start] - 'a']) {
count--;
}
start++;
} else {
freqS[s[end] - 'a']++;
if(freqS[s[end] - 'a'] <= freqT[s[end] - 'a']) {
count++;
}
end++;
}
}
while(count == t.length()) {
if(answer > end - start) {
answer = end - start;
result = s.substr(start, end - start);
}
freqS[s[start] - 'a']--;
if(freqS[s[start] - 'a'] < freqT[s[start] - 'a']) {
count--;
}
start++;
}
cout << result << endl;
return 0;
}