Algorithm Notes
Summary: Longest Repeating Character Replacement — notes not yet curated.
Time: Estimate via loops/recurrences; common classes: O(1), O(log n), O(n), O(n log n), O(n^2)
Space: Count auxiliary structures and recursion depth.
Tip: See the Big-O Guide for how to derive bounds and compare trade-offs.
Big-O Guide
Source
"""
Longest Repeating Character Replacement
TODO: Add problem description
"""
from collections import defaultdict
from src.interview_workbook.leetcode._registry import register_problem
from src.interview_workbook.leetcode._types import Category, Difficulty
class Solution:
def solve(self, *args):
"""Return length of longest substring with at most k replacements."""
s, k = args
count = defaultdict(int)
res = 0
l = 0
maxf = 0
for r, ch in enumerate(s):
count[ch] += 1
maxf = max(maxf, count[ch])
while (r - l + 1) - maxf > k:
count[s[l]] -= 1
l += 1
res = max(res, r - l + 1)
return res
def demo():
"""TODO: Implement demo function."""
pass
register_problem(
id=424,
slug="longest_repeating_character_replacement",
title="Longest Repeating Character Replacement",
category=Category.SLIDING_WINDOW,
difficulty=Difficulty.MEDIUM,
tags=["string", "sliding_window"],
url="https://leetcode.com/problems/longest-repeating-character-replacement/",
notes="",
)