-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.cpp
58 lines (47 loc) · 1.51 KB
/
solution.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
#include <iostream>
#include <string>
#include <unordered_map>
#include <climits>
using namespace std;
string minWindow(string s, string t) {
if (s.empty() || t.empty())
return "";
// Dictionary to store counts of characters in t
unordered_map<char, int> dictT;
for (char c : t)
dictT[c]++;
// Number of unique characters in t that must be present in the window
int required = dictT.size();
// Sliding window counters
unordered_map<char, int> windowCounts;
int formed = 0;
// (window length, left, right)
int minLen = INT_MAX, minLeft = 0;
int l = 0;
// Expand the window with right pointer
for (int r = 0; r < s.size(); r++) {
char c = s[r];
windowCounts[c]++;
if (dictT.count(c) && windowCounts[c] == dictT[c])
formed++;
// Try to contract the window till the point it's no longer 'desirable'
while (l <= r && formed == required) {
// Update smallest window if smaller found
if (r - l + 1 < minLen) {
minLen = r - l + 1;
minLeft = l;
}
char d = s[l];
windowCounts[d]--;
if (dictT.count(d) && windowCounts[d] < dictT[d])
formed--;
l++;
}
}
return minLen == INT_MAX ? "" : s.substr(minLeft, minLen);
}
int main() {
string s = "ADOBECODEBANC", t = "ABC";
cout << "Minimum Window Substring: " << minWindow(s, t) << endl;
return 0;
}