#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
vector<int> a(n + 1);
for(int i = 1; i <= n; i++) {
char x;
cin >> x;
if(x == 'J') {
a[i] = 1;
}
if(x == 'O') {
a[i] = 2;
}
if(x == 'I') {
a[i] = 3;
}
}
int ans = 0;
int cnt1 = 0;
int l = - 1, r = - 1;
for(int i = 1; i <= n; i++) {
if(a[i] == 1) {
cnt1 += 1;
}
if(a[i] != 1 && cnt1) {
ans += 1;
}
if(a[i] == 1 && cnt1 == k) {
l = i + 1;
break;
}
}
int cnt3 = 0;
for(int i = n; i >= 1; i--) {
if(a[i] == 3) {
cnt3 += 1;
}
if(a[i] != 3 && cnt3) {
ans += 1;
}
if(a[i] == 3 && cnt3 == k) {
r = i - 1;
break;
}
}
if(l > r) {
cout << - 1;
return 0;
}
if(l == - 1 || r == - 1) {
cout << - 1;
return 0;
}
int solpref = 0;
int solsuf = 0;
int cnt2 = 0;
int ok1 = 0;
int ok2 = 0;
for(int i = l; i <= r; i++) {
if(a[i] == 2) {
cnt2 += 1;
}
else {
solpref += 1;
}
if(cnt2 == k) {
ok1 = 1;
break;
}
}
cnt2 = 0;
for(int i = r; i >= l; i--) {
if(a[i] == 2) {
cnt2 += 1;
}
else {
solsuf += 1;
}
if(cnt2 == k) {
ok2 = 1;
break;
}
}
vector<int> P;
if(ok1) {
P.push_back(solpref);
}
if(ok2) {
P.push_back(solsuf);
}
if(!P.size()) {
cout << - 1;
return 0;
}
sort(P.begin(), P.end());
cout << ans + P[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... |