Sliding Window Flashcards

1
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Longest Repeating Character Replacement

You are given a string s consisting of only uppercase english characters and an integer k. You can choose up to k characters of the string and replace them with any other uppercase English character.

After performing at most k replacements, return the length of the longest substring which contains only one distinct character.

Input: s = "XYYX", k = 2

Output: 4
A
  1. Expand the window by moving r and updating the frequency of characters.
  2. Track the most frequent character count by using a dictionary
    ```
    for r in range(len(s)):
    countDict[s[r]] += 1
    maxFreq = max(maxFreq, countDict[s[r]])
    ~~~
  3. If (window size - maxFreq) >= k, shrink the window from the left (l++).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly