-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec6_Smallest_window_0_1_2.cpp
More file actions
82 lines (72 loc) Β· 1.72 KB
/
Copy pathlec6_Smallest_window_0_1_2.cpp
File metadata and controls
82 lines (72 loc) Β· 1.72 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
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
https://www.geeksforgeeks.org/problems/smallest-window-containing-0-1-and-2--170637/1
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class Solution {
public:
int smallestSubstring(string S) {
// Code here
vector<int>count(256 , 0);
int first = 0 ,second = 0 ,len = S.size();
int diff = 3; // 0 , 1, 2
int diff1= 0;
while(first<S.size())
{
if(count[S[first]]==0)
{
diff1++;
}
count[S[first]]++;
first++;
}
if(diff1<diff)
{
return -1;
}
for(int i = 0 ; i<count.size();i++)
{
count[i] = 0 ;
}
first = 0 ;
while(second<S.size())
{
//diff exist
while(diff && second<S.size())
{
if(count[S[second]] == 0)
{
diff--;
}
count[S[second]]++;
second ++;
}
len = min(len , second - first);
while(diff!=1)
{
len = min(len , second - first);
count[S[first]]--;
if(count[S[first]]==0)
{
diff++;
}
first++;
}
}
return len;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
string S;
cin >> S;
Solution ob;
cout << ob.smallestSubstring(S);
cout << endl;
}
}
// } Driver Code Ends