O(n)处理即可。
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 100000;
const int inf = 0x3f3f3f3f;
char s[maxn];
int cnt[30];
deque<int> dq;
int cur_cnt, n;
int main() {
int ans = inf;
scanf("%s", s);
n = strlen(s);
cur_cnt = 0;
for(int i = 0; i < n; ++i) {
dq.push_back(i);
if(cnt[s[i]-'A'] == 0) {
cur_cnt++;
}
cnt[s[i]-'A']++;
if(cur_cnt >= 26) {
while(cur_cnt >= 26) {
ans = min(ans, dq.back() - dq.front() + 1);
if(--cnt[s[dq.front()]-'A'] == 0) {
--cur_cnt;
}
dq.pop_front();
}
}
}
if(ans < inf) printf("%d\n", ans);
else printf("No Solution\n");
return 0;
}