Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. class Solution:
  2. def characterReplacement(self, s: str, k: int) -> int:
  3. if not s:
  4. return 0
  5. if k > len(s) - 2:
  6. return len(s)
  7.  
  8. a = [-k for i in range(26)]
  9. b = [0 for i in range(26)]
  10.  
  11. numC = [0 for i in range(26)]
  12.  
  13.  
  14.  
  15. best = k + 1
  16.  
  17. for i in range(len(s)):
  18. c = ord(s[i]) - ord('A')
  19. numC[c] += 1
  20.  
  21. cost = i - b[c]
  22. b[c] = i + 1
  23.  
  24.  
  25. oldC = 0
  26. j = 0
  27.  
  28.  
  29. while j < cost:
  30. if a[c] + j < 0:
  31. j += 1
  32. else:
  33. char2 = s[a[c] + j + oldC]
  34. if char2 == s[i]:
  35. oldC += 1
  36. else:
  37. j += 1
  38.  
  39. numC[c] -= oldC
  40. a[c] += oldC + j
  41.  
  42. best = max(best, numC[c] + k)
  43.  
  44.  
  45. return min(best, len(s))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement