-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay28.cpp
More file actions
56 lines (44 loc) · 1.29 KB
/
Day28.cpp
File metadata and controls
56 lines (44 loc) · 1.29 KB
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
/*
This problem was asked by Amazon.
Given a string, find the length of the smallest window that contains every distinct character. Characters may appear more than once in the window.
For example, given "jiujitsu", you should return 5, corresponding to the final five letters. |
*/
#include <bits/stdc++.h>
using namespace std;
int min_window(string s)
{
int n = s.size();
if (n == 0)
return 0;
unordered_set<char> uniqueChars(s.begin(), s.end());
int totalUnique = uniqueChars.size();
unordered_map<char, int> windowCount;
int minLen = INT_MAX;
int start = 0;
int uniqueInWindow = 0;
for (int end = 0; end < n; ++end)
{
char rightChar = s[end];
windowCount[rightChar]++;
if (windowCount[rightChar] == 1)
uniqueInWindow++;
while (uniqueInWindow == totalUnique)
{
int windowSize = end - start + 1;
if (windowSize < minLen)
minLen = windowSize;
char leftChar = s[start];
windowCount[leftChar]--;
if (windowCount[leftChar] == 0)
uniqueInWindow--;
start++;
}
}
return minLen;
}
int main()
{
string s = "jiujitsu";
cout << "Minimum window length: " << min_window(s) << endl;
return 0;
}