forked from saibighnesh/basic_programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSLIDING WINDOW TECHNIQUE.cpp
More file actions
68 lines (57 loc) · 990 Bytes
/
SLIDING WINDOW TECHNIQUE.cpp
File metadata and controls
68 lines (57 loc) · 990 Bytes
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
// AMAN MISHRA (IIT KGP) : SLIDING WINDOW TECHNIQUE
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll arr[200001];
ll sliding_window(ll n)
{
ll left=0;
ll right=0;
map<ll,ll> m;
m[arr[0]]++;
ll count=0;
ll size=n+1;
while(right<n&&left<n)
{
// m[arr[r]]++;
if(m[1]>=1&&m[2]>=1&&m[3]>=1)
{
size=min(size,right-left+1);
count++;
m[arr[left]]--;
left++;
}
else
{
right++;
m[arr[right]]++;
}
}
return size;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
ll t;
cin>>t;
while(t--)
{
string s;
cin>>s;
ll n=s.length();
for(ll i=0;i<n;i++)
{
arr[i]=s[i]-'0';
}
ll a=sliding_window(n);
if(a==n+1)
{
cout<<0<<"\n";
}
else
{
cout<<a<<"\n";
}
}
}