424. 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


def characterReplacement(self, s: str, k: int):
       
    L = len(s)
    left = 0
    right = 0
    count = [0]*26
    max_count = 0
    max_len = 0
   
    while right < L:
       
        c = ord(s[right]) - ord('A')
        count[c] += 1
        max_count = max(max_count, count[c])            
       
       
        if right - left + 1 - max_count > k and left < right:
            c = ord(s[left]) - ord('A')                
            count[c] -= 1
            left += 1
           
        max_len = max(max_len, right - left + 1)
       
        right += 1
       
    return max_len

Comments

Popular posts from this blog

849. Maximize Distance to Closest Person

347. Top K Frequent Elements

139. Word Break