Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <cmath>
- #include <set>
- #include <vector>
- #include <queue>
- #include <algorithm>
- using namespace std;
- typedef long long ll;
- int main() {
- ios_base::sync_with_stdio(false);
- int n, k;
- cin >> n >> k;
- string s;
- cin >> s;
- multiset<int> ms;
- for(int i = 0; i < 26; i++) {
- ms.insert(0);
- }
- vector<int> cnt(26, 0);
- int i = 0, j = 0;
- int question_marks = 0;
- int chars = 0;
- while(j < n) {
- if(*ms.begin() <= k and *ms.rbegin() <= k) {
- if(s[j] == '?') {
- question_marks++;
- }
- else {
- chars++;
- ms.erase(ms.find(cnt[s[j] - 'a']));
- cnt[s[j] - 'a']++;
- ms.insert(cnt[s[j] - 'a']);
- }
- j++;
- if(chars + question_marks == 26 * k) {
- cout << i + 1 << endl;
- return 0;
- }
- }
- else {
- if(s[i] == '?') {
- question_marks--;
- }
- else {
- chars--;
- ms.erase(ms.find(cnt[s[i] - 'a']));
- cnt[s[i] - 'a']--;
- ms.insert(cnt[s[i] - 'a']);
- }
- i++;
- if(chars + question_marks == 26 * k) {
- cout << i << endl;
- return 0;
- }
- }
- }
- cout << -1 << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement