↓Skip to main content
  1. LeetCode/

LeetCode 424: Longest Repeating Character Replacement

·2 mins· ·
LeetCode Medium Hash-Table String Sliding-Window
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-10-28

  • Submissions: Accepted ×4 → Runtime Error → Accepted

Approach
#

To make the window a single character, keep the most frequent one and replace the other length - maxCount. Extend the right end by one each step and shrink the left by one when the replacements needed exceed k (Sliding Window Template 2). With 26 letters, max(count.values()) is O(26).

My Attempts
#

It passed in 2022; one 2026 rewrite raised an RE: max(count) returned a character key that was compared with an integer (TypeError), and shrinking used count[i] (the index, not the character). Fixing both passed.

Solution
#

class Solution:
    def characterReplacement(self, s: str, k: int) -> int:
        count = defaultdict(int)
        res = l = 0
        for r, ch in enumerate(s):
            count[ch] += 1
            while (r - l + 1) - max(count.values()) > k:
                count[s[l]] -= 1
                l += 1
            res = max(res, r - l + 1)
        return res

Complexity
#

Time O(26n), space O(26).

Mistakes to Avoid
#

  • When shrinking, decrement count[s[l]] (the character), not count[l] (the index).
  • max(count) is the largest key; the largest count is max(count.values()).

Interview Explanation
#

I use a variable sliding window: window length minus the top character count is the number of replacements; when it exceeds k I shrink from the left, tracking the longest window. With 26 letters, time is O(26n).

Related

LeetCode 981: Time Based Key-Value Store
·2 mins
LeetCode Medium Hash-Table String Binary-Search Design
LeetCode 981 note: store a (timestamp, value) list per key, with timestamps increasing; get binary searches the last timestamp <= the query
LeetCode 49: Group Anagrams
·2 mins
LeetCode Medium Array Hash-Table String Sorting
LeetCode 49 note: use each string’s sorted form as a key; strings sharing a key form one anagram group
LeetCode 17: Letter Combinations of a Phone Number
·2 mins
LeetCode Medium Hash-Table String Backtracking
LeetCode 17 note: each level handles one digit and tries its letters; an empty input returns an empty list
LeetCode 128: Longest Consecutive Sequence
·2 mins
LeetCode Medium Array Hash-Table Union-Find
LeetCode 128 note: put everything in a set and count upward only from values whose x - 1 is absent; each value is seen at most twice, O(n)
LeetCode 347: Top K Frequent Elements
·1 min
LeetCode Medium Array Hash-Table Divide-and-Conquer Sorting
LeetCode 347 note: count first, then take the k most frequent with a size-k heap or a bucket sort indexed by count
LeetCode 79: Word Search
·3 mins
LeetCode Medium Array String Backtracking Depth-First-Search
LeetCode 79 note: backtrack from every cell, matching the next character in four directions with visited cells marked temporarily and restored; prune early with character counts