Welcome to Subscribe On Youtube

424. Longest Repeating Character Replacement

Description

You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times.

Return the length of the longest substring containing the same letter you can get after performing the above operations.

 

Example 1:

Input: s = "ABAB", k = 2
Output: 4
Explanation: Replace the two 'A's with two 'B's or vice versa.

Example 2:

Input: s = "AABABBA", k = 1
Output: 4
Explanation: Replace the one 'A' in the middle with 'B' and form "AABBBBA".
The substring "BBBB" has the longest repeating letters, which is 4.
There may exists other ways to achieve this answer too.

 

Constraints:

  • 1 <= s.length <= 105
  • s consists of only uppercase English letters.
  • 0 <= k <= s.length

Solutions

  • class Solution {
        public int characterReplacement(String s, int k) {
            int[] counter = new int[26];
            int i = 0;
            int j = 0;
            for (int maxCnt = 0; i < s.length(); ++i) {
                char c = s.charAt(i);
                ++counter[c - 'A'];
                maxCnt = Math.max(maxCnt, counter[c - 'A']);
                if (i - j + 1 - maxCnt > k) {
                    --counter[s.charAt(j) - 'A'];
                    ++j;
                }
            }
            return i - j;
        }
    }
    
  • class Solution {
    public:
        int characterReplacement(string s, int k) {
            vector<int> counter(26);
            int i = 0, j = 0, maxCnt = 0;
            for (char& c : s) {
                ++counter[c - 'A'];
                maxCnt = max(maxCnt, counter[c - 'A']);
                if (i - j + 1 > maxCnt + k) {
                    --counter[s[j] - 'A'];
                    ++j;
                }
                ++i;
            }
            return i - j;
        }
    };
    
  • class Solution:
        def characterReplacement(self, s: str, k: int) -> int:
            counter = [0] * 26
            i = j = maxCnt = 0
            while i < len(s):
                counter[ord(s[i]) - ord('A')] += 1
                maxCnt = max(maxCnt, counter[ord(s[i]) - ord('A')])
                if i - j + 1 > maxCnt + k:
                    counter[ord(s[j]) - ord('A')] -= 1
                    j += 1
                i += 1
            return i - j
    
    
  • func characterReplacement(s string, k int) int {
    	counter := make([]int, 26)
    	j, maxCnt := 0, 0
    	for i := range s {
    		c := s[i] - 'A'
    		counter[c]++
    		if maxCnt < counter[c] {
    			maxCnt = counter[c]
    		}
    		if i-j+1 > maxCnt+k {
    			counter[s[j]-'A']--
    			j++
    		}
    	}
    	return len(s) - j
    }
    
  • function characterReplacement(s: string, k: number): number {
        const idx = (c: string) => c.charCodeAt(0) - 65;
        const cnt: number[] = Array(26).fill(0);
        const n = s.length;
        let [l, mx] = [0, 0];
        for (let r = 0; r < n; ++r) {
            mx = Math.max(mx, ++cnt[idx(s[r])]);
            if (r - l + 1 - mx > k) {
                --cnt[idx(s[l++])];
            }
        }
        return n - l;
    }
    
    

All Problems

All Solutions