1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// @Title: 替换后的最长重复字符 (Longest Repeating Character Replacement)
// @Author: 15816537946@163.com
// @Date: 2021-02-02 23:13:06
// @Runtime: 0 ms
// @Memory: 2.4 MB
func characterReplacement(s string, k int) int {
	cnt := [26]int{}
	maxCnt, left := 0, 0

	for right, ch := range s {
		cnt[ch-'A']++
		maxCnt = max(maxCnt, cnt[ch-'A'])
		if right-left+1 > k+maxCnt {
			cnt[s[left]-'A']--
			left++
		}
	}
	return len(s) - left

}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}