On leetcode 424. Longest Repeating Character Replacement
-
(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.
Here's my solution:
func characterReplacement(s string, k int) int {
var out int
for i, _ := range s {
c := make(map[byte]int)
count := 0
for j:= i; j < len(s); j++ {
count +=1
c[s[j]] += 1
// determine max freq
maxFreq := 0
for _, v := range c {
if v > maxFreq {
maxFreq = v
}
}
if count - maxFreq <= k {
if count > out {
out = count
}
}
}
}
return out
}
Excluding the determine max freq
loop, this type of sliding window algorithm is considered O(n), but I can't see why that is, to me it should be O(n^2).
Can someone help me understand why the time complexity is O(n) and not O(n^2)?