nikunjsoni

1234

Apr 10th, 2021
95
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2. public:
  3.     int balancedString(string s) {
  4.         unordered_map<char, int> count;
  5.         int left, right, n=s.length(), k=n/4;
  6.        
  7.         for(int i=0; i<n; i++)
  8.             count[s[i]]++;
  9.        
  10.         int ans = n;
  11.         for(left=0, right=0; right<n; right++){
  12.             count[s[right]]--;
  13.             while(left<n && count['Q']<=k && count['W']<=k && count['E']<=k && count['R']<=k){
  14.                 ans = min(ans, right-left+1);
  15.                 count[s[left++]] += 1;
  16.             }
  17.         }
  18.         return ans;
  19.     }
  20. };
RAW Paste Data