Source Code
#include <stdio.h>

int main(){
    long long n;
    scanf("%lld",&n);
    char x[n];
  scanf("%s",x);
    long long  index = -1,count = 0; 
  if(x[0] == x[n-1]) {printf("%lld",n-1);
    return 0;}
  else{
    for(int i = 1; i < n-1; i++){
      if(x[0] == x[i] ) index = i;
    }
    if(index == -1) {count = 0;
      index = 0;}
    else count += index;
    long index2 = -1;
    for(int i = n-2; i > index; i--){
      if(x[n-1] == x[i]) index2 = i;
    }
    if(index2 != -1) count+= (n-index2-1);
    
    printf("%lld",count);}
}
Copy
Pop Pop Ayoooy
GNU G++17
2 ms
976 KB
Accepted