forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1.cpp
More file actions
28 lines (28 loc) · 919 Bytes
/
s1.cpp
File metadata and controls
28 lines (28 loc) · 919 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
// OJ: https://leetcode.com/problems/minimum-window-substring/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
class Solution {
public:
string minWindow(string s, string t) {
int cnt = 0, i = 0, j = 0, S = s.size(), T = t.size();
int minLen = INT_MAX, begin = -1;
unordered_map<char, int> m, seen;
for (char c : t) m[c]++;
while (j < S) {
for (; j < S && cnt != T; ++j) {
if (m.find(s[j]) == m.end()) continue;
if (++seen[s[j]] <= m[s[j]]) ++cnt;
}
for (; cnt == T; ++i) {
if (m.find(s[i]) == m.end()) continue;
if (j - i < minLen) {
minLen = j - i;
begin = i;
}
if (--seen[s[i]] < m[s[i]]) --cnt;
}
}
return begin == -1 ? "" : s.substr(begin, minLen);
}
};