-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec6_SmallestDistinctWindow.cpp
85 lines (70 loc) Β· 1.8 KB
/
lec6_SmallestDistinctWindow.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
https://www.geeksforgeeks.org/problems/smallest-distant-window3132/1?page=1&difficulty
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
public:
int findSubString(string str)
{
// Your code goes here
vector<int>count(256, 0 ) ;
int first = 0 , second = 0 , len = str.size() , diff = 0 ;
// calculate unique chars
while(first<str.size())
{
if(count[str[first]] == 0 )
{
diff++;
}
count[str[first]]++;
first++;
}
//set to zero
for(int i = 0 ;i<256;i++)
{
count[i] = 0 ;
}
first = 0 ;
while(second < str.size())
{
//diff exists , increase the size
while(diff && second < str.size())
{
if(count[str[second]] == 0)
{
diff--;
}
count[str[second]]++;
second++;
}
len = min(len , second - first );
// decrease the size
while(diff!=1)
{
len = min(len , second - first );
count[str[first]]--;
if(count[str[first]] == 0)
{
diff++;
}
first++;
}
}
return len;
}
};
//{ Driver Code Starts.
// Driver code
int main() {
int t;
cin >> t;
while (t--) {
string str;
cin >> str;
Solution ob;
cout << ob.findSubString(str) << endl;
}
return 0;
}
// } Driver Code Ends