neetcode-gh/leetcode

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

Closed this issue · 1 comments

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

Thanks for the feedback! The testcases has been added.