#K35347. Longest Substring with At Most \(m\) 'b's

    ID: 25511 Type: Default 1000ms 256MiB

Longest Substring with At Most \(m\) 'b's

Longest Substring with At Most (m) 'b's

Given an integer \(m\) and a string \(s\) consisting only of the characters 'a' and 'b', your task is to determine the length of the longest contiguous substring of \(s\) that contains no more than \(m\) occurrences of 'b'.

Explanation: A substring is a contiguous segment of a string. For example, if \(s = \texttt{abbaabba}\) and \(m = 2\), one optimal substring is \(\texttt{aabba}\) which has length 5 and contains exactly 2 'b's.

Constraints: The string \(s\) contains only 'a's and 'b's, and \(m\) is a non-negative integer.

inputFormat

The input is given via stdin and consists of two lines:

  • The first line contains a non-negative integer \(m\), indicating the maximum number of 'b's allowed in the substring.
  • The second line contains the string \(s\) which is composed solely of the characters 'a' and 'b'.

outputFormat

Output a single integer to stdout — the maximum length of a contiguous substring of \(s\) that contains at most \(m\) 'b's.

## sample
2
abbaabba
5