Leetcode - Longest Repeating Character Replacement
https://leetcode.com/problems/longest-repeating-character-replacement/
Given a string s
that consists of only uppercase English letters, you can perform at most k
operations on that string.
In one operation, you can choose any character of the string and change it to any other uppercase English character.
Find the length of the longest sub-string containing all repeating letters you can get after performing the above operations.
Note:
Both the string's length and k will not exceed 104.
Example 1:
Input: s = "ABAB", k = 2
Similar with Max Consecutive III
func characterReplacement(s string, k int) int { count := make([]int, 26) left := 0 right := 0 maxNum := 0 result := 0 for right < len(s) { val := s[right] count[val - 'A']++ if maxNum < count[val - 'A'] { maxNum = count[val - 'A'] } for right - left + 1 - maxNum > k { count[s[left] - 'A']-- left++ for i := 0; i < 26; i++ { if maxNum < count[i] { maxNum = count[i] } } } result = max(result, right - left + 1) right++ } return result } func max(a, b int) int { if a > b { return a } return b }