https://leetcode.com/problems/longest-repeating-character-replacement/
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.
var (size = len(s) // s字符串的长度fre = make(map[byte]int, 26) // map记录窗口中每个字符出现的次数, 因为题目中说了只有大写字母,所以map的长度为26maxLen = 0 // 最长的子串的长度maxCount = 0 // 窗口中出现次数最多的字符的次数)
func characterReplacement(s string, k int) int {for left, right := 0, 0; right < size; right++ {fre[s[right]]++ // 右指针指向的字符在map中的次数加1if maxCount < fre[s[right]] { // 更新maxCountmaxCount = fre[s[right]]}if right-left+1-maxCount > k { // 如果窗口的长度减去出现次数最多的字符的次数大于k,那么就需要移动左指针fre[s[left]]-- // 左指针指向的字符在map中的次数减1left++ // 窗口左指针右移}if maxLen < right-left+1 { // 更新maxLenmaxLen = right - left + 1}}return maxLen
}