Minimum Recolors to Get K Consecutive Black Blocks

EasyStringSliding WindowGreedyMath

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:

Sliding window of size 7: the window with minimum W's is 'WBBWBW' with 3 whites. Recolor those 3 to get 7 consecutive blacks.

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:

The best length-5 window has exactly 1 white block, so recoloring that one block gives 5 consecutive black blocks. The exact output is 1.

Constraints

  • 1 ≤ blocks.length ≤ 100

Ready to solve this problem?

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