-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSmallest distinct window.cpp
50 lines (45 loc) · 1.04 KB
/
Smallest distinct window.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
#include<bits/stdc++.h>
using namespace std;
int smallestWindow(string);
int main(){
ios_base::sync_with_stdio(false);
int t;
string str;
cin >> t;
while(t--){
cin >> str;
cout << smallestWindow(str) << endl;
}
return 0;
}
int smallestWindow(string str){
unordered_set<char> us;
unordered_map<char, int> um;
queue<char> q;
int min = INT_MAX;
for(int i = 0; i < str.length(); i++)
us.insert(str[i]);
for(int i = 0; i < str.length(); i++){
q.push(str[i]);
um[str[i]]++;
if(um.size() == us.size()){
if(min > q.size())
min = q.size();
char temp = q.front();
um[temp]--;
if(um[temp] == 0)
um.erase(temp);
q.pop();
}
while(1){
char temp = q.front();
if(um.find(temp) != um.end() && um[temp] > 1){
um[temp]--;
q.pop();
}
else
break;
}
}
return min;
}