Coins
Time limit1sMemory limit128 MB
Given a string of O and R tosses, find the longest substring where the number of O equals k times the number of R.
- Level
Medium6 of 10
- Topics
- Prefix sum, Hash map, Math
- Solved
- No attempts yet
Problem
Joe claims that he has telekinetic powers. This shocked Stan, a committed rationalist, who immediately asked Joe to prove it.
Joe decided to demonstrate his ability by tossing a coin. He says he can toss it so that heads come up exactly times as often as tails. Stan wrote down the result of every toss in order, and now he wants to find the longest run of consecutive tosses in which the number of heads is exactly times the number of tails.
Input
The first line contains two integers and (, ). Here is the number of tosses Joe made, and has the meaning described above.
The second line contains a string of characters describing the outcome of each toss. Each character is either O for heads or R for tails.
Output
Print a single integer: the length of the longest run of consecutive tosses in which heads occur exactly times as often as tails. If no such run exists, print .
Hint
In the sample input, the tosses from position 5 through 12 and from position 6 through 13 each contain exactly 6 heads and 2 tails, that is, three times as many heads as tails. No longer consecutive run has this property, so the answer is 8.