Skip to content

Bug Report for longest-repeating-substring-with-replacement #6109

Description

@stepant74

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))

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions