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

#define FIO ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
typedef long long ll;
typedef long double ld;

using namespace std;

const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const ll INF = 1e18;

int main()
{
    FIO
    int n, ans = 0, a = 0, b = 0;
    string s;
    cin >> n >> s;
    for (int i = 0; i < n; i++)
    {
        if (s[i] == s[0]) a = i;
        if (s[i] == s.back()) b = i;
        if (a <= b && i)
            ans = max(ans, a + (n - b - 1));
    }
    cout << ans << '\n';
    return 0;
}
Copy
Pop Pop Mariam_Yasser
GNU G++17
1 ms
904 KB
Accepted