6 line C++ O(n) (NOT O(26n)) Time O(26) Space Solution


  • 0
    F
    struct Solution {
        int characterReplacement(string s, int k) {
            int counts[26] = {0}, i = 0, j = 0, maxval = 0;
            for (;j < s.size(); ++ j)
                if ((maxval = max(maxval, ++ counts[s[j] - 'A'])) < j - i + 1 - k)
                    counts[s[i ++] - 'A'] --;
            return j - i;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.