Skip to content

Instantly share code, notes, and snippets.

@rtsoy
Created January 5, 2026 18:23
Show Gist options
  • Select an option

  • Save rtsoy/5da30c93f872d9efac50e908acfa3739 to your computer and use it in GitHub Desktop.

Select an option

Save rtsoy/5da30c93f872d9efac50e908acfa3739 to your computer and use it in GitHub Desktop.
424. Longest Repeating Character Replacement
// https://leetcode.com/problems/longest-repeating-character-replacement/
//
// Time: O(n)
// Space: O(1)
//
// n = number of letters in string
// .................... //
func characterReplacement(s string, k int) int {
n := len(s)
res := 0
letters := [26]int{}
maxfreq := 0
l, r := 0, 0
for r < n {
ridx := s[r] - 'A'
letters[ridx]++
maxfreq = max(maxfreq, letters[ridx])
subs := (r - l + 1) - maxfreq
if subs > k {
lidx := s[l] - 'A'
letters[lidx]--
l++
}
res = max(res, r - l + 1)
r++
}
return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment