Educational Codeforces Round 87 (Rated for Div. 2) 1354B - Ternary String Solution
Solution in C++:
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t,i,j;
cin>>t;
while(t--)
{
string s;
int a=-1,b=-1,c=-1,ans=1e9,mx,mn;
cin>>s;
for(i=0; i<s.size(); i++)
{
if(s[i]=='1') a=i;
else if(s[i]=='2') b=i;
else if(s[i]=='3') c=i;
if(a>=0&&b>=0&&c>=0)
{
mx=max(a,max(b,c));
mn=min(a,min(b,c));
ans=min(ans,(mx-mn+1));
}
}
if(ans==1e9)
cout<<"0"<<endl;
else
cout<<ans<<endl;
}
}
No comments