-2

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)?

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
NimaKapoor
  • 119
  • 6
  • 1
    You're the one who claims it's O(n), so you tell us. – Kelly Bundy Jan 02 '22 at 03:33
  • 2
    In general, if the window slides in some "smart" way that makes the algorithm O(n), then we call it a "sliding window" algorithm; if all possible windows are considered and the algorithm is Ө(n²), then we call it a "bruteforce" algorithm ;-) But note that "sliding window" is a bit vague and ambiguous, and has been used for pretty different types of algorithms. – Stef Jan 02 '22 at 10:12

1 Answers1

3

The way you implemented the algorithm, the complexity is O(n^2). If you claim that "this type of algorithm is considered O(n)", then you didn't correctly implement this algorithm.

Here is a O(n) solution for this problem, written in JavaScript (source):

var characterReplacement = function(s, k) {
    // Keep count of all the characters in the string
    const chars = {}
    // left pointer, character with the current max frequency, return value
    let left = 0, maxf = 0, output = 0
    for(let right = 0; right < s.length; right++) {
        const char = s[right]
        // Increment count of the current character
        chars[char] = 1 + (chars[char] || 0)
        // Update the character frequency
        maxf = Math.max(maxf, chars[char])
        // Shrink the window of characters we are looking at until we can have a window of all the same characters + k charcters to change
        while((right-left+1) - maxf > k) {
                chars[s[left]] -= 1
                left++
          }
        // Update the output if the current window is greater than our previous max window
        output = Math.max(output, right - left +1)
    }
    return output
};
Visne
  • 357
  • 3
  • 10