Longest Repeating Character Replacement - JS Solution

JS
Editorial

Solution explanation

Solution explanation

Approach

We solve the problem using a Sliding Window technique with two pointers (left and right) and a frequency map that tracks character counts within the current window. The goal is to find the longest substring where, after replacing at most k characters, all remaining characters in the window become identical.

  • left: Marks the beginning of the current candidate window.
  • right: Moves forward to expand the window by examining the next character.

Algorithm Flow:

  • The process iterates while right has not reached the end of the string.
  • At the start of each iteration, attempt to expand the window by including s[right]:
    • Increment its frequency in the map.
    • Update maxFreq β€” the highest frequency of any single character currently in the window.
  • Compute:
    • windowSize = right βˆ’ left + 1
    • replacementsNeeded = windowSize βˆ’ maxFreq
    If replacementsNeeded ≀ k, the current window is valid and may represent a new maximum length.
  • If replacementsNeeded > k:
    • The current window is invalid. Move left forward to cancel the latest expansion but shift the window instead, and decrease the frequency of s[left] as it leaves the window. This doesn’t necessarily make the window valid immediately β€” it simply repositions the window while preserving the length of the last valid window, allowing future expansions to be attempted again.
  • Continue expanding: After adjusting left, increment right to test the next potential expansion.
  • Throughout the process, right keeps advancing until it reaches the end of the string, while left moves forward only when needed to cancel an invalid expansion. This ensures that the algorithm always preserves the right βˆ’ left distance of the last valid windowβ€”except for the final increment of right that terminates the loop. Therefore, at the end, the length of the longest valid substring equals inputString.length (the final value of right) βˆ’ left.

Why we don’t need to recompute maxFreq every time:

  • When left moves forward, maxFreq might become overestimated because removing characters can only reduce their frequency.
  • This overestimation is harmless:
    • The previous maxFreq already failed to make the window valid for its current size.
    • Until maxFreq naturally increases again through new additions on the right, the window cannot incorrectly appear valid.
  • Thus, keeping an occasionally outdated maxFreq preserves correctness while improving efficiency.

🏁 Once the entire string is processed, the window distance from left to the end represents the length of the longest valid substring achievable with at most k replacements.

Time Complexity

Let n be the length of the input string. Both pointers left and right traverse the string at most once.

  • Each character is added to the window once when right expands.
  • Each character is removed from the window at most once when left moves forward.
  • Updating the frequency map and maintaining maxFreq both take O(1) time per operation.

Therefore, the overall time complexity is O(n).

Space Complexity

The algorithm maintains a frequency map (charFreq) that stores the count of each character currently inside the window. Since s consists of only uppercase English letters, the map can store at most 26 entries, which is asymptotically O(1).

All other variables (left, right, maxFreq) also use O(1) space. Hence, the total auxiliary space is O(1).

Visucodize editorial solution titled JS Solution for LeetCode's Longest Repeating Character Replacement coding problem. Includes code and may include explanation. You can animate the code step by step using the provided input by clicking the run button, or fork it locally to update the code and input for custom visualization. View the original problem at https://leetcode.com/problems/longest-repeating-character-replacement.