kucheasysa

Algoverse_adesh_8

Jun 4th, 2024
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. class Solution:
  2.         def characterReplacement(self, s: str, k: int) -> int:
  3.        
  4.             l = 0
  5.             frequency = {}
  6.             longest = 0
  7.             for i in range(len(s)):
  8.                
  9.                 if not s[i] in frequency:
  10.                     frequency[s[i]] = 0
  11.                 frequency[s[i]] += 1
  12.                 count = i - l + 1
  13.                 if count - max(frequency.values()) <= k:
  14.                     longest = max(longest, count)
  15.                    
  16.                 else:
  17.                     frequency[s[l]] -= 1
  18.                     if not frequency[s[l]]:
  19.                         frequency.pop(s[l])
  20.                     l += 1
  21.            
  22.             return longest
  23.            
Advertisement
Add Comment
Please, Sign In to add comment