**Physical Address**

304 North Cardinal St.

Dorchester Center, MA 02124

You are given a **0-indexed** string `blocks`

of length `n`

, where `blocks[i]`

is either `'W'`

or `'B'`

, representing the color of the `i`

block. The characters ^{th}`'W'`

and `'B'`

denote the colors white and black, respectively.

You are also given an integer `k`

, which is the desired number of **consecutive** black blocks.

In one operation, you can **recolor** a white block such that it becomes a black block.

Return* the minimum number of operations needed such that there is at least one occurrence of *

`k`

**Example 1:**

```
Input: blocks = "WBBWWBBWBW", k = 7
Output: 3
Explanation:
One way to achieve 7 consecutive black blocks is to recolor the 0th, 3rd, and 4th blocks
so that blocks = "BBBBBBBWBW".
It can be shown that there is no way to achieve 7 consecutive black blocks in less than 3 operations.
Therefore, we return 3.
```

**Example 2:**

```
Input: blocks = "WBWBBBW", k = 2
Output: 0
Explanation:
No changes need to be made, since 2 consecutive black blocks already exist.
Therefore, we return 0.
```

**Constraints:**

`n == blocks.length`

`1 <= n <= 100`

`blocks[i]`

is either`'W'`

or`'B'`

.`1 <= k <= n`

```
int minimumRecolors(string blocks, int k) {
int back = 0, front = 0, count_w = 0, ans = INT_MAX;
while(front < blocks.size()){
if(blocks[front] == 'W'){ count_w++; }
if(front - back + 1 >= k){
ans = min(ans, count_w);
if(blocks[back] == 'W') count_w--;
back++;
}
front++;
}
return ans;
}
```

```
public int minimumRecolors(String blocks, int k) {
int min = Integer.MAX_VALUE;
for (int lo = -1, hi = 0, white = 0; hi < blocks.length(); ++hi) {
white += blocks.charAt(hi) == 'W' ? 1 : 0;
if (hi - lo >= k) { // the window reaches size of k.
min = Math.min(min, white); // update minimum.
// slide 1 step right the lower bound of the sliding
// window and update the value of white count.
white -= blocks.charAt(++lo) == 'W' ? 1 : 0;
}
}
return min;
}
```

```
class Solution:
def minimumRecolors(self, blocks: str, k: int) -> int:
n, minOps, flips, count, i = len(blocks), 1e9, 0, 0, 0
for j in range (n):
if blocks[j] == 'W':
flips += 1
count += 1
elif blocks[j] == 'B':
count += 1
if count == k:
minOps = min(minOps, flips)
if blocks[i] == 'W':
flips -= 1
count -= 1
else: count -= 1
i += 1
return minOps
```

In our experience, we suggest you solve this Minimum Recolors to Get K Consecutive Black Blocks LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Minimum Recolors to Get K Consecutive Black Blocks LeetCode Solution

I hope this Minimum Recolors to Get K Consecutive Black Blocks LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

**More Coding Solutions >>**