Source Code
#include <bits/stdc++.h> 
#define int long long
#define f first
#define s second
#define pb push_back
#define endl "\n"
#define SORT(v) sort(begin(v), end(v))
using namespace std;
int32_t main(){ ios::sync_with_stdio(false);cin.tie(0);
    int n;cin >> n;
    string s;cin >> s;
    char ff = s[0];
    char ss = s[n-1];
    
    int last = -1;
    int ans = 5;
    for(int i = 0;i<n;i++){
        if(s[i] == ff){
            last = i;
        }
        if(s[i] == ss){
            ans = min(ans,i-last+1);
        }
    }
    cout << n - ans;
}
Copy
Pop Pop barakat
GNU G++17
39 ms
1.2 MB
Accepted