Skip to the code
cf.
+10 XP

#130 Fewest Recolors for a Solid Run

easy

A pixel-art editor shows a row of pixels as a string row of "B" (black) and "W" (white). The user wants k black pixels in a row somewhere and can recolor white pixels black, one at a time.

Write fewestRecolors(row, k) that returns the smallest number of white pixels that must be recolored so that some k consecutive pixels are all black. k is never more than row.length.

Example 1

Input: row = "WBBWWBBWBW", k = 7
Output: 3

The 7 pixels from index 0 to 6, WBBWWBB, hold three whites. No window of 7 holds fewer.

Example 2

Input: row = "WBWBBBW", k = 2
Output: 0

BB already exists.

Constraints

  • 1 <= k <= row.length <= 100000
  • row contains only B and W.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).