Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def characterReplacement(self, s: str, k: int) -> int:
- chars_in_the_current_window = defaultdict(int)
- longest_substring = 0
- l = 0
- for r in range(len(s)):
- chars_in_the_current_window[s[r]] += 1
- while _chars_other_than_the_most_common(l, r, chars_in_the_current_window) > k:
- chars_in_the_current_window[s[l]] -= 1
- l += 1
- longest_substring = max(longest_substring, _the_window_size(l, r))
- return longest_substring
- def _chars_other_than_the_most_common(l, r, chars_in_the_current_window):
- return _the_window_size(l, r) - _the_number_of_occurrences_of_the_most_common_character(chars_in_the_current_window)
- def _the_window_size(l, r):
- return r - l + 1
- def _the_number_of_occurrences_of_the_most_common_character(chars_in_the_current_window):
- return max(chars_in_the_current_window.values())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement