jayati

Longest Repeating Character Replacement

Mar 28th, 2023
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int characterReplacement(string s, int k) {
  4.         int maxLen = 0, windowStart = 0, maxRepeatLetterCount = 0;
  5.         unordered_map<char,int> charFreq;
  6.         for(int windowEnd = 0; windowEnd<s.size(); windowEnd++){
  7.             char rightChar = s[windowEnd];
  8.             charFreq[rightChar]++;
  9.             maxRepeatLetterCount = max(maxRepeatLetterCount,charFreq[rightChar]);
  10.            
  11.             if(windowEnd - windowStart + 1 - maxRepeatLetterCount > k){
  12.                 char leftChar = s[windowStart];
  13.                 charFreq[leftChar]--;
  14.                 windowStart++;
  15.             }
  16.             maxLen = max(maxLen,windowEnd-windowStart+1);
  17.         }
  18.         return maxLen;
  19.     }
  20. };
Advertisement
Add Comment
Please, Sign In to add comment