Source Code
#include <bits/stdc++.h>

#define IO  ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
#define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define endl '\n'
#define f first
#define s second
#define ll long long
#define ld long double
#define mod 1000000007
#define INF 0x3f3f3f3f3f3f3f3f

using namespace std;
const int M = 5e3 + 5, C = 1e5 + 5;

int main() {
    IO
    int n;
    string s,t;
    cin>>n>>s;
    t=s;
    reverse(t.begin(),t.end());
    int pos=t.find(s.front());
    pos=s.size()-(pos+1);
    int ans=pos;
    pos=0;
    for(int i=1;i<t.find(s.front());i++){
        if(t[i]==s.back()){
            //ans+=i;
            pos=i;
            //break;
        }
    }
    cout<<ans+pos;
    return 0;
}
Copy
Pop Pop Sarah_Montasser
GNU G++17
1 ms
980 KB
Accepted