-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_window_substring.cpp
62 lines (55 loc) · 1.78 KB
/
min_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
62
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s = "ADOBECODEBANC"; // input string ;
string pattern = "BANC"; // pattern string
unordered_map<char, int> mp; // map to store the frequency of each char in pattern string
int i, j, count, min_length; // declaring variables
i = j = 0; // initial value of both the window pointers
min_length = INT_MAX; // minimum length of window
int start, end; // to output the substring
// Loop to create the map of frequency of each letter in pattern string
for (int i = 0; i < pattern.size(); i++)
mp[pattern[i]]++;
count = mp.size(); // number of unique characters available in pattern string
// sliding window loop
while (j < s.length())
{
if (count > 0)
{
if (mp.find(s[j]) != mp.end())
{
mp[s[j]]--;
if (mp[s[j]] == 0)
count--;
}
}
if (count == 0)
{
// Loop to reduce the window size to minimum.
while (count == 0)
{
min_length = min(min_length, j - i + 1); // updating the minimum window size
// Keeping start and end pointers to keep updated the output string at last.
start = i;
end = j;
if (mp.find(s[i]) != mp.end())
{
mp[s[i]]++;
if (mp[s[i]] > 0)
count++;
}
i++;
}
}
j++;
}
cout << "Minimum length of window is : " << min_length << endl
<< "Substring : ";
for (int i = start; i <= end; i++)
{
cout << s[i];
}
return 0;
}