Source Code
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define TEST int t;cin>>t;while(t--)
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define fr first
#define sc second
#define popcount __builtin_popcountll
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const ll mod=1000000007;
typedef vector<int,int> p1;
typedef vector<ll,ll> p2;

int main()
{
    IOS;
    //TEST{

  int n;
  cin>>n;
  string s;
  cin>>s;
  int id1,id2,mi=1e9;
  char a=s[0],b=s[n-1];
  bool l=0;
  for(int i=0;i<s.length();i++){
        if(a==b){
                if(s[i]==a){
            if(!l){
                id1=i;
                l=1;
            }
            else{
                id2=i;
                mi=min(mi,id2-id1);
                id1=id2;
            }
                }
        }
        else{
       if(s[i]==a){
        id1=i;
       }
        if(s[i]==b){
        id2=i;
        mi=min(mi,id2-id1);
       }
        }
  }
  if(mi==1e9)cout<<0;
  else cout<<n-(mi+1);



cout<<endl;
//}
    return 0;
}
Copy
Pop Pop AlCoach
GNU G++17
1 ms
416 KB
Wrong Answer