-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-window-substring.cc
62 lines (54 loc) · 1.79 KB
/
minimum-window-substring.cc
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
56
57
58
59
60
61
62
#include "leetcode.h"
using namespace std;
class Solution {
public:
string minWindow(const string &str, const string &tar) {
unordered_map<char, bool> map;
for (auto ch : tar) { map[ch] = false; }
auto m_width = INT32_MAX, m_l = 0, m_r = 0, counter = 0, cur = 0;
deque<int> dq;
for (; cur < str.length() && dq.size() < tar.length(); ++cur) {
if (auto it = map.find(str[cur]); it != map.end()) {
dq.push_front(cur);
if (!it->second) {
it->second = true;
++counter;
}
}
}
if (dq.size() < tar.size()) { return ""; }
if (counter == tar.size()) {
m_r = dq.front() + 1;
m_l = dq.back();
m_width = m_r - m_l;
}
for (; cur < str.length(); ++cur) {
if (auto it = map.find(str[cur]); it != map.end()) {
dq.push_front(cur);
const auto b = dq.back();
if (map[b]) {
--counter;
map[b] = false;
}
dq.pop_back();
if (!it->second) {
it->second = true;
++counter;
}
if (counter == tar.size()) {
const auto width = dq.front() - dq.back() + 1;
if (width < m_width) {
m_width = width;
m_r = dq.front() + 1;
m_l = dq.back();
}
}
}
}
return m_r > m_l ? str.substr(m_l, m_r) : "";
}
};
int main(int argc, char const *argv[]) {
Solution solution;
cout << solution.minWindow("aa", "aa");
}