sandeep03edu

De Shaw C - String

Jul 21st, 2022
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5.     // your code goes here
  6.    
  7.     // Ques input
  8.     string str = "ababkeca";
  9.     int k = 2;
  10.    
  11.     // Soln
  12.     int left = 0, right = 0;
  13.     int n = str.length();
  14.    
  15.     unordered_map<char, int> mp;
  16.     bool flag = 1;
  17.     int ans = k+1;
  18.    
  19.     while(left<n && right<n){
  20.         if(flag){
  21.             mp[str[right]]++;
  22.         }
  23.        
  24.         vector<int> count(26, 0);
  25.         for(int i=0; i<26; ++i){
  26.             char a = 'a' + i;
  27.             count[i] = mp[a];
  28.         }
  29.        
  30.         sort(count.begin(), count.end());
  31.        
  32.         int maxi1 = count[25], maxi2 = count[24];
  33.         int sum = accumulate(count.begin(), count.end(), 0);
  34.        
  35.         if(maxi1==0 || maxi2==0 || sum- maxi1 - maxi2<=k){
  36.             ans = max(ans , right - left + 1);
  37.             right++;
  38.             flag = 1;
  39.         }
  40.         else if(sum- maxi1 - maxi2 > k){
  41.             mp[str[left++]]--;
  42.             flag = 0;
  43.         }
  44.        
  45.     }
  46.  
  47.     cout<<ans<<endl;
  48.  
  49.     return 0;
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment