#include <bits/stdc++.h>
using namespace std;
void test_case() {
int n;
cin >> n;
string s;
cin >> s;
set<char> ss;
for (int i = 0; i < n; ++i) {
ss.insert(s[i]);
}
int unique = (int)ss.size();
vector<bool> vis(26, false);
vector<int> num_unique(n+1);
num_unique[0] = 0;
for (int i = 0; i < n; ++i) {
if (!vis[s[i]]) {
num_unique[i+1] = num_unique[i] + 1;
vis[s[i]] = true;
}
else {
num_unique[i+1] = num_unique[i];
}
}
int i = 1, j = n, maxi = 0, ans = 0;
while (i <= j) {
int dist = num_unique[j]-num_unique[i];
if (dist >= maxi) {
maxi = dist;
ans = j-i+1;
}
if (i+1 < n+1 && num_unique[i+1] == num_unique[i]) {
++i;
}
else if (j-1 > i && num_unique[j-1] == num_unique[j]) {
--j;
}
else {
++i;
}
}
cout << ans << '\n';
}
int main(int argc, char const *argv[]) {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int tc = 1;
// cin >> tc;
while (tc--) {
test_case();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnZvaWQgdGVzdF9jYXNlKCkgewogIGludCBuOwogIGNpbiA+PiBuOwogIHN0cmluZyBzOwogIGNpbiA+PiBzOwogIHNldDxjaGFyPiBzczsKICBmb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkgewogICAgc3MuaW5zZXJ0KHNbaV0pOwogIH0KICBpbnQgdW5pcXVlID0gKGludClzcy5zaXplKCk7CiAgdmVjdG9yPGJvb2w+IHZpcygyNiwgZmFsc2UpOwogIHZlY3RvcjxpbnQ+IG51bV91bmlxdWUobisxKTsKICBudW1fdW5pcXVlWzBdID0gMDsKICBmb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkgewogICAgaWYgKCF2aXNbc1tpXV0pIHsKICAgICAgbnVtX3VuaXF1ZVtpKzFdID0gbnVtX3VuaXF1ZVtpXSArIDE7CiAgICAgIHZpc1tzW2ldXSA9IHRydWU7CiAgICB9CiAg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