I'm trying to solve Leetcode 424. Longest Repeating Character Replacement. Why is this code not working, I cannot get my head around it.
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
l, r = 0, 0
res = 0
char_count = {}
while r < len(s):
char_count[s[r]] = char_count.get(s[r], 0) + 1
substr_len = r - l + 1
if substr_len - max(char_count.values()) <= k:
res = max(res, substr_len)
r += 1
else:
char_count[s[l]] -= 1
l += 1
return res
Test case:
Input
s =
"AABABBA"
k =
1
Output
5
Expected
4
While this works:
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
l, r = 0, 0
res = 0
char_count = {}
while r < len(s):
char_count[s[r]] = char_count.get(s[r], 0) + 1
substr_len = r - l + 1
r += 1
if substr_len - max(char_count.values()) <= k:
res = max(res, substr_len)
else:
char_count[s[l]] -= 1
l += 1
return res
The difference between the two codes is where the right pointer is incremented. Shouldn't the right pointer only be incremented if the substr_len - max(char_count.values()) <= k
? Isn't this what the first code is doing?, while the second code always increments the right pointer, even if substr_len > k
?