#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define name "a"
using namespace std;
using ii = pair<int,int>;
using aa = array<int,3>;
const int N = 1e5+5;
char a[N];
int pre[3][N];
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,k;
cin >> n >> k;
for(int i=1;i<=n;i++) {
cin >> a[i];
for(int j=0;j<3;j++) {
pre[j][i]=pre[j][i-1];
}
if(a[i]=='J') {
pre[0][i]++;
} else
if(a[i]=='O') {
pre[1][i]++;
}
else {
pre[2][i]++;
}
}
int ans=1e9;
for(int i=1;i<=n;i++) {
int x1=lower_bound(pre[0]+1,pre[0]+1+n,pre[0][i-1]+k)-pre[0];
int x2=lower_bound(pre[1]+1,pre[1]+1+n,pre[1][x1]+k)-pre[1];
int x3=lower_bound(pre[2]+1,pre[2]+1+n,pre[2][x2]+k)-pre[2];
if(max({x1,x2,x3})<=n) {
ans=min(ans,x3-i+1-3*k);
}
//cout << x1 << ' ' << x2 << ' ' << x3 << endl;
}
if(ans==1e9) cout << -1; else
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |