Source Code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end() 

int const N = 2e5+100;

int main(){
    int n;
    string s; 
    cin >> n >> s; 
    int an = 0, j = 0, t = 0;
    char f = s[0], l = s[n-1];
    if(f == l)return cout << n-1 <<"\n", 0;
    for(int i = 0; i<n; ++i){
       if(s[i] == f)an = i, j = i; 
    }
    for(int i = n-1; i > j; --i){
      if(s[i] == l)t = i ;
    }
    cout << an + (n-t-1) <<"\n";

}
Copy
Pop Pop molukhyyeh
GNU G++17
1 ms
864 KB
Accepted