-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00076-minimum_window_substring.cpp
61 lines (45 loc) · 1.18 KB
/
00076-minimum_window_substring.cpp
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
// 76: Minimum Window Substring
// https://leetcode.com/problems/minimum-window-substring/
#include <iostream>
#include <unordered_map>
using namespace std;
class Solution {
public:
// SOLUTION
string minWindow(string s, string t) {
unordered_map<char, int> m;
for (int i=0; i<t.size(); i++) m[t[i]]++;
int i = 0;
int j = 0;
int counter = t.size();
int minStart = 0;
int minLength = INT16_MAX;
while (j < s.size()) {
if (m[s[j]] > 0) counter--;
m[s[j]]--;
j++;
while (counter==0) {
if (j - i < minLength) {
minStart = i;
minLength = j-i;
}
m[s[i]]++;
if (m[s[i]] > 0) counter++;
i++;
}
}
if (minLength != INT16_MAX)
return s.substr(minStart, minLength);
return "";
}
};
int main() {
Solution o;
// INPUT
string s = "ADOBECODEBANC";
string t = "ABC";
// OUTPUT
auto result = o.minWindow(s, t);
cout<<result<<endl;
return 0;
}