I looked up a previous explaination but I was unable to understand it.
How to go about solving this problem
Hello @sushant19_99
For a range with maximum ‘a’ characters (after changing at most k characters from b to a)
Maintain two variables “start” and “end” to denote the range in which their are some a’s and maximum of k b’s.
Maintain two more variables cntA = 0 and cntB = 0
Maintain a maxRange intialized to 0
initialize start = 0 and end = 0
Loop through the string
If the character is ‘a’ then increment cntA and increment end
if the character is ‘b’ then increment cntB and increment end
if(cntB > k) {
increment start and reduce cntA and cntB until cntB <= k because we are only allowed to change k
characters.
while(cntB > k) {
if(string[start] == ‘a’) cntA–;
else cntB–;
}
update maxRange = max(maxRange, end - start + 1);
Do the same thing
For a range with maximum ‘b’ characters (after changing at most k characters from a to b)
…
Let me know if you need any help.