-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.h
55 lines (54 loc) · 1.41 KB
/
solution.h
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
/*
Code generated by https://github.com/goodstudyqaq/leetcode-local-tester
*/
#if __has_include("../utils/cpp/help.hpp")
#include "../utils/cpp/help.hpp"
#elif __has_include("../../utils/cpp/help.hpp")
#include "../../utils/cpp/help.hpp"
#else
#define debug(...) 42
#endif
class Solution {
public:
string minWindow(string s, string t) {
unordered_map<char, int> M;
unordered_set<char> S;
for (auto it : t) {
M[it]++;
S.insert(it);
}
int r = 0;
int n = s.size();
int ans = n + 1;
int idxl = -1, idxr = -1;
int now_score = 0;
for (int i = 0; i < n; i++) {
while (r < n && now_score < S.size()) {
char it = s[r];
if (S.count(it)) {
M[it]--;
if (M[it] == 0) {
now_score++;
}
}
r++;
}
if (now_score < S.size()) break;
if (ans > r - i) {
ans = r - i;
idxl = i;
idxr = r;
}
char it = s[i];
if (S.count(it)) {
M[it]++;
if (M[it] > 0) {
now_score--;
}
}
}
if (ans == n + 1) return "";
string res = s.substr(idxl, idxr - idxl);
return res;
}
};