Bug Report for https://neetcode.io/problems/longest-repeating-substring-with-replacement
Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.
This code passes the tests, but it is wrong. It fails on strings like:
s="AAABAAAA", k=0
s="AAABBAAAAA", k=1
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
#s="AAABAAAA"
#k=0
m = 1
h = {}
for i, c in enumerate(s):
if c in h:
h[c][1] += 1
if h[c][1] == 2:
h[c][2] = i
if i - h[c][0] - h[c][1] + 1 > k:
h[c][1] -= 1
if h[c][2]:
h[c][0] = h[c][2]
else:
h[c][2] = i
if h[c][1] == 2:
h[c][2] = i
if h[c][1] > m:
m = h[c][1]
else:
h[c] = [i, 1, None]
return min(m + k, len(s))
Bug Report for https://neetcode.io/problems/longest-repeating-substring-with-replacement
Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.
This code passes the tests, but it is wrong. It fails on strings like:
s="AAABAAAA", k=0
s="AAABBAAAAA", k=1
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
#s="AAABAAAA"
#k=0
m = 1
h = {}
for i, c in enumerate(s):
if c in h:
h[c][1] += 1
if h[c][1] == 2:
h[c][2] = i
if i - h[c][0] - h[c][1] + 1 > k:
h[c][1] -= 1
if h[c][2]:
h[c][0] = h[c][2]
else:
h[c][2] = i
if h[c][1] == 2:
h[c][2] = i
if h[c][1] > m:
m = h[c][1]
else:
h[c] = [i, 1, None]
return min(m + k, len(s))