Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- // your code goes here
- // Ques input
- string str = "ababkeca";
- int k = 2;
- // Soln
- int left = 0, right = 0;
- int n = str.length();
- unordered_map<char, int> mp;
- bool flag = 1;
- int ans = k+1;
- while(left<n && right<n){
- if(flag){
- mp[str[right]]++;
- }
- vector<int> count(26, 0);
- for(int i=0; i<26; ++i){
- char a = 'a' + i;
- count[i] = mp[a];
- }
- sort(count.begin(), count.end());
- int maxi1 = count[25], maxi2 = count[24];
- int sum = accumulate(count.begin(), count.end(), 0);
- if(maxi1==0 || maxi2==0 || sum- maxi1 - maxi2<=k){
- ans = max(ans , right - left + 1);
- right++;
- flag = 1;
- }
- else if(sum- maxi1 - maxi2 > k){
- mp[str[left++]]--;
- flag = 0;
- }
- }
- cout<<ans<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment