nathanwailes

LeetCode 424 - Longest Repeating Character Replacement - NeetCode solution

Oct 14th, 2023 (edited)
694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.44 KB | None | 0 0
  1. class Solution:
  2.     def characterReplacement(self, s: str, k: int) -> int:
  3.         count = {}
  4.         res = 0
  5.  
  6.         l = 0
  7.         maxf = 0
  8.         for r in range(len(s)):
  9.             count[s[r]] = 1 + count.get(s[r], 0)
  10.             maxf = max(maxf, count[s[r]])
  11.  
  12.             while (r - l + 1) - maxf > k:
  13.                 count[s[l]] -= 1
  14.                 l += 1
  15.            
  16.             res = max(res, r - l + 1)
  17.         return res
Advertisement
Add Comment
Please, Sign In to add comment