-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path76.cpp
More file actions
37 lines (33 loc) · 1.08 KB
/
76.cpp
File metadata and controls
37 lines (33 loc) · 1.08 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
class Solution {
public:
int char2idx(char c) {
if (c >= 'a' && c <= 'z') return c - 'a';
if (c >= 'A' && c <= 'Z') return c - 'A' + 26;
return -1;
}
string minWindow(string s, string t) {
int m = s.size();
int n = t.size();
if (n > m) return "";
vector<int> tCount(52, 0);
for (auto& c : t) tCount[char2idx(c)]++;
int remaining = n;
int minLen = INT_MAX;
int begin = -1;
int left = 0;
for (int right = 0; right < m; ++right) {
if (tCount[char2idx(s[right])] > 0) remaining--;
tCount[char2idx(s[right])]--;
while (remaining == 0) {
if (right - left + 1 < minLen) {
minLen = right - left + 1;
begin = left;
}
if (tCount[char2idx(s[left])] == 0) remaining++;
tCount[char2idx(s[left])]++;
left++;
}
}
return (minLen == INT_MAX) ? "" : s.substr(begin, minLen);
}
};