Minimum Size Subarray Sum
This commit is contained in:
39
src/s0076_minimum_window_substring.cpp
Normal file
39
src/s0076_minimum_window_substring.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include "s0076_minimum_window_substring.hpp"
|
||||
|
||||
bool Solution::check() {
|
||||
for (const auto &p : ori) {
|
||||
if (cnt[p.first] < p.second) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// 不包含 t 中所有字符,r 右移
|
||||
// 包含 t 中所有字符,l 右移
|
||||
string Solution::minWindow(string s, string t) {
|
||||
for (const auto &c : t) {
|
||||
++ori[c];
|
||||
}
|
||||
|
||||
int l = 0, r = -1;
|
||||
int len = INT_MAX, ansL = -1, ansR = -1;
|
||||
|
||||
while (r < int(s.size())) {
|
||||
if (ori.find(s[++r]) != ori.end()) {
|
||||
++cnt[s[r]];
|
||||
}
|
||||
while (check() && l <= r) {
|
||||
if (r - l + 1 < len) {
|
||||
len = r - l + 1;
|
||||
ansL = l;
|
||||
}
|
||||
if (ori.find(s[l]) != ori.end()) {
|
||||
--cnt[s[l]];
|
||||
}
|
||||
++l;
|
||||
}
|
||||
}
|
||||
|
||||
return ansL == -1 ? string() : s.substr(ansL, len);
|
||||
}
|
Reference in New Issue
Block a user