Source Code
#include<bits/stdc++.h>
#define all(s) s.begin(), s.end()
#define sz(s) (int) s.size()
using namespace std;
void DeathRoad(){
    int n; cin >> n;
    string s; cin >> s;
    vector<int> left(n + 1), right(n + 1);
    int indexToFirst =  1, indexToLast = n;
    for (int i = 0; i < n; i++) {
        if(s[i] == s.front())
            indexToFirst = i + 1;
        left[i] = indexToFirst;
    }
    for (int i = n - 1; i >= 0; i--){
        if(s[i] == s.back())
            indexToLast = i + 1;
        right[i] = indexToLast;
    }
    int ans = 0;
    for (int i = 0; i < n; i++){
        ans = max(ans, n - right[i] + left[i] - 1);
    }
    cout << ans;
    
}
int main() {
    ios_base::sync_with_stdio(0);
    
    int T = 1;
    //cin >> T;
    while (T--)
        DeathRoad();
    return 0;
}
/***
        We're here for death, so let's go to it together!
***/
 
Copy
Pop Pop MhmudAbdGhafar
GNU G++17
1 ms
1.7 MB
Accepted