一. 题目描述
二. 测试用例
三. 代码
#include <iostream>
using namespace std;
#include <cmath>
#include <vector>
int main(){
int n,k;
cin >> n >> k;
string ss;
cin >> ss;
vector<int> count[26];
for(int i=0;i<ss.size();i++){
count[ss[i]-'a'].push_back(i);
}
bool flag = false;
int ans = n;
for(int i=0;i<26;i++){
if(count[i].size() >= k){
for(int j=0;j<count[i].size()-k+1;j++){
int foo = count[i][j+k-1]-count[i][j]+1;
if(foo <= ans){
flag = true;
ans = foo;
}
}
}
}
if(flag){
cout << ans << endl;
}else{
cout << -1 << endl;
}
return 0;
}