-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_STOII_0017_minWindow.cc
More file actions
55 lines (53 loc) · 1017 Bytes
/
Copy pathProblem_STOII_0017_minWindow.cc
File metadata and controls
55 lines (53 loc) · 1017 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
49
50
51
52
53
54
55
#include <iostream>
#include <vector>
using namespace std;
// 与 leetcode 0076 相同
// https://leetcode-cn.com/problems/minimum-window-substring/
// see at Problem_0076_minWindow.cc
class Solution
{
public:
string minWindow(string s, string t)
{
int n = s.length();
int m = t.length();
vector<int> cnt(256);
for (auto &c : t)
{
cnt[c]++;
}
int left = 0;
int right = 0;
int all = m;
int len = -1;
int lpos = -1;
int rpos = -1;
while (right < n)
{
cnt[s[right]]--;
if (cnt[s[right]] >= 0)
{
all--;
}
if (0 == all)
{
while (cnt[s[left]] < 0)
{
cnt[s[left]]++;
left++;
}
if (len == -1 || len > right - left + 1)
{
len = right - left + 1;
lpos = left;
rpos = right;
}
all++;
cnt[s[left]]++;
left++;
}
right++;
}
return len == -1 ? "" : s.substr(lpos, len);
}
};