Minimum Recolors to Get K Consecutive Black Blocks

Easy

Description

Given a string blocks where W is white and B is black, and an integer k, find the minimum number of white blocks needed to recolor to get k consecutive black blocks.

Examples

Input:blocks = "WBBWWBBWBW", k = 7
Output:3
Explanation:

Recolor 3 W's.

Input:blocks = "WBWBBBW", k = 2
Output:0
Explanation:

The substring "BB" at positions 3-4 already contains 2 consecutive black blocks, so no recoloring is needed.

Input:blocks = "BBBBWWWBBB", k = 5
Output:1
Explanation:

5 consecutive black blocks are needed. The substring "BBBBW" (positions 0-4) has 4 B's and 1 W, so recoloring 1 W gives 5 consecutive B's.

Constraints

  • 1 ≤ blocks.length ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!