Tags: LeetCode DSA Sliding Window
Prompt:
Longest Repeating Character Replacement
MediumTopicsCompany TagsHints
You are given a string s consisting of only uppercase english characters and an integer k. You can choose up to k characters of the string and replace them with any other uppercase English character.
After performing at most k replacements, return the length of the longest substring which contains only one distinct character.
Example 1:
Input: s = "XYYX", k = 2
Output: 4Explanation: Either replace the ‘X’s with ‘Y’s, or replace the ‘Y’s with ‘X’s.
Example 2:
Input: s = "AAABABB", k = 1
Output: 5Constraints:
1 <= s.length <= 100,0000 <= k <= s.lengthsconsists of only uppercase english characters.
Solution:
class Solution {
public:
int characterReplacement(string s, int k)
{
int left = 0;
int biggestCharCount = 0;
int best = 0;
//a count array initialized with the count of zero;
int count[26] = {0};
//iterate over the entire string
for(int i = 0; i < s.length(); i++)
{
//always increment the right, and then add this character to the array count
count[s[i] - 'A']++;
//update the largest count of a character if you come across it.
if(count[s[i] - 'A'] > biggestCharCount)
{ biggestCharCount = count[s[i]- 'A']; }
//do the left check
int windowSize = i - left + 1;
int difference = windowSize - biggestCharCount; //letter not most common in array
while(difference > k)
{
//decrement the left
count[s[left] - 'A']--;
left ++;
windowSize = i - left + 1;
difference = windowSize - biggestCharCount;
}
//std::cout << "Biggest Char Count: " << biggestCharCount << std::endl;
//std::cout << "WindowSize: " << windowSize << std::endl;
//std::cout << "difference: " << difference << std::endl;
if(best < windowSize) {best = windowSize;}
}
return best;
}
};
Explanation:
So this one was a bit wacky and I feel like I do not fully get it but here is how the solution goes, at least to my general understanding. So basically you know how with the previous sliding window you kind of moved the right forward each time and then you updated the left whenever a certain condition was hit? It is basically the same thing here, except a bit different.
So the general idea is to count what the most common letter you have is, and then after doing so, subtract that from the current size of the window, this lets you find the number of all of the characters you would need to replace to make the perfect string, and in doing so, you can compare this number to k in order to find if you have enough replacements.
If you do have enough replacements, you can just increase the window size, if you do not however, then what you need to do is decrease the window size from the left until the difference is alright again.
As for the actual code there is more nuance to it, basically that count array is pretty important because you need to know which of your letters is the most common, so you need to update it with a count of each letter you find and if this count is greater than the current best you update it. You need to however decrement this count whenever you slide the window over, so that way if the same letter is encountered again it is not mistaken for the most common letter. Doing this lets you go through all of this in a single o(n) time complexity. The reason for the best variable is also there to ensure that you have a max that remains as large as it needs to, the window does not need to shrink down multiple times if the difference becomes greater than k, because then we know it would eventually not be larger than our current best guess, a bit weird for sure, but a cool problem.