Skip to content

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

Closed
@khris190

Description

@khris190

Bug Report for https://neetcode.io/problems/longest-repeating-substring-with-replacement

i've an algorithm passing all of the tests that is not correct:

class Solution {
public:
    int characterReplacement(string s, int k) {
        int left = 0;
        int maxLen = 0;
        int tmpK = k;
        for(int right = 1; right < s.size(); right++){
            if(s[right] != s[left]){
                tmpK--;
                if(tmpK < 0){
                    do{
                        left++;
                    }while(s[left] == s[left-1]);
                    right = left;
                    tmpK = k;
                }
            }
            if(maxLen < right - left + 1){
                maxLen = right - left + 1;
            }
        }
        int backLen = s.size() - left + tmpK;
        if(maxLen < backLen){
            maxLen = backLen;
        }
        if(maxLen > s.size()){
            maxLen = s.size();
        }
        return maxLen;
    }
};

that code passes all of the tests but it's not 100% correct as it fails on a case
s="ABCDZZZ"
k=3
and
s="ABCDZZ"
k=3
i'd propose adding at least one of those test cases into the question

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