Educational Codeforces Round 87 (Rated for Div. 2) 1354B - Ternary String Solution

 


 Solution in C++:

/// La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
/// Abul Hasnat  Tonmoy
/// Department of CSE,23rd batch
/// Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///

#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

Most View Post

Recent post

RESTful APIs with CRUD Operations in Laravel 12| (2025)

  RESTful APIs serve as the foundation of modern web development. They follow a set of rules called Representational State Transfer (REST) t...

Powered by Blogger.