#include #define MAX 500000 int main() { int count; char str[MAX]; std::bitset charPresence[30]; scanf("%d ", &count); //memset(str, 'a', MAX); fgets(str, MAX, stdin); for(int i = 0; i < count; i++) str[i] -= 'a'; for(int i = 0; i < count; i++) charPresence[str[i]].set(i); int symLength = 0; std::bitset valid; valid.set(); for(int i = count - 1; i >= 0; i--) { char c = str[i]; valid &= charPresence[c]; valid <<= 1; if(valid[count - 1]) symLength = count - i; if(!valid.any()) break; } printf("%d", count - symLength - 1); return 0; }