I use the sliding window technique to find the minimum number of white blocks ('W') that need to be changed in any substring of length k.
I then iterate through all possible windows of size k in the string and count the number of 'W' characters, keeping track of the minimum count.
class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
min_operations = float('inf')
for i in range(len(blocks) - k + 1):
window = blocks[i:i + k]
operations = window.count('W')
min_operations = min(min_operations, operations)
return min_operations
- Time: O(n)
- Space: O(1)
