#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int n;
cin >> n;
string s;
cin >> s;
int ans = 0;
vector<int> idx;
char a = s.front();
char b = s.back();
for (int i = n - 1; i >= 0; --i) {
if (s[i] == b) idx.push_back(i);
}
for (int i = 0; i < n; ++i) {
while (idx.size() && idx.back() <= i) idx.pop_back();
if (s[i] == a && idx.size()) {
ans = max(ans, i + (n - idx.back() - 1));
}
}
cout << ans << '\n';
return 0;
}
Copy