Add a slower one, but easy to understand


  • 0
    Z
    public class Solution {
    public int characterReplacement(String s, int k) {
        char[] map = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
        int length = 0;
        for(int i = 0; i < 26; i++){
            int count = 0;
            char c = map[i];
            int left = 0;
            for(int right = 0; right < s.length(); right++){// We try every letter
                if(s.charAt(right) != c){
                    count++;
                }
                while(count > k){// keeps a valid window always
                    if(s.charAt(left) != c){
                        count--;
                    }
                    left++;
                }
                length = Math.max(length, right - left + 1);
            }
        }
        return length;
    }
    

    }


Log in to reply
 

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