Junpyo's Pebbles
Time limit1sMemory limit512 MB
Find the longest contiguous segment containing at most B black pebbles and at least W white pebbles.
- Level
Medium6 of 10
- Topics
- Two pointers, Sliding window, Prefix sum
- Solved
- No attempts yet
Problem
pebbles stand in a row, numbered from to . Each pebble is black or white.
Junpyo chooses one contiguous segment and picks up every pebble inside it. The segment may be empty, which means going straight home. Its length is the number of pebbles it contains.
A segment is valid when it holds at most black pebbles and at least white pebbles. If no valid segment exists, the answer is . Find the length of the longest valid segment.
Input
The first line contains , the upper bound for black pebbles, and the lower bound for white pebbles, separated by spaces.
The second line contains a string of length . Its -th character describes pebble : B means black and W means white.
Output
Print the length of the longest valid segment on one line. If no segment satisfies the conditions, print .