Sliding Window Flashcards
1
Q
A
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
- Expand the window by moving r and updating the frequency of characters.
- 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]])
~~~ - If (window size - maxFreq) >= k, shrink the window from the left (l++).
3
Q
A