#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
	ll n, m, r, x, y, ans,i,p, j,cnt1,cnt, t,s, mx, mn, k, o;
	
	cin >> n >> k;
	
	string str;
	
	cin >> str;
	
	vector < ll > J, O, I;
	for (i = 0; i < n; i ++) {
		if ( str[i] == 'J') J.push_back(i);
		if ( str[i] == 'O') O.push_back(i);
		if ( str[i] == 'I') I.push_back(i);
	}
	ans = -1;
	for (j =k - 1; j < J.size(); j++) {
		o = lower_bound(O.begin(), O.end(), J[j]) - O.begin();
		o = o + k - 1;
		if ( o >= O.size()) continue;
		i = lower_bound(I.begin(), I.end(), O[o]) - I.begin();
		i = i + k - 1;
		if( i >= I.size()) continue;
		if ( ans == -1) ans = I[i] - J[j - k + 1] + 1;
		else ans =min(ans, I[i] - J[j- k + 1] + 1);
	}
	if ( ans == -1) {
		cout << ans << endl;
		return 0;
	} 
	cout << ans - (3 * k)<< endl;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |