Alternating Strings (APCS 2017-10 Intermediate)

Points 100 1.0s 256M

A string consisting entirely of uppercase English letters is called an uppercase string. A string consisting entirely of lowercase English letters is called a lowercase string.

Given a positive integer \(k\), a string is called a \(k\)-alternating string if it is formed by alternately concatenating uppercase and lowercase strings, each of length exactly \(k\). The first block may have either case, and a single block is also valid.

For example, StRiNg is a \(1\)-alternating string, and heLLow is a \(2\)-alternating string. For \(k=2\) and input string aBBaaa, the longest contiguous \(k\)-alternating substring is BBaa, whose length is \(4\).

Find the longest contiguous substring of the given string that is a \(k\)-alternating string.

Input Format

The first line contains an integer \(k\).

The second line contains a nonempty string \(s\) consisting only of English letters AZ and az, with no spaces.

Constraints

  • \(1 \le k \le 100000\)
  • \(1 \le |s| \le 100000\)
  • \(s\) contains only uppercase and lowercase English letters.

Output Format

Print the length of the longest contiguous \(k\)-alternating substring of \(s\). Print 0 if none exists.

Scoring

The four samples are judged but worth zero points. There are exactly \(20\) scored groups worth \(5\) points each:

  • Groups \(1\)–\(4\) (\(20\) points): \(|s|\le 20\) and \(k=1\).
  • Groups \(5\)–\(10\) (\(30\) points): \(|s|\le 100\) and \(k\le 2\).
  • Groups \(11\)–\(20\) (\(50\) points): no additional constraints.

Sample Input 1

1
aBBdaaa

Sample Output 1

2

Sample Input 2

3
DDaasAAbbCC

Sample Output 2

3

Sample Explanation 2

A valid answer may contain just one uppercase or lowercase block of length \(k\); a case change is not required.

Sample Input 3

2
aafAXbbCDCCC

Sample Output 3

8

Sample Input 4

3
DDaaAAbbCC

Sample Output 4

0

Source

APCS October 2017, implementation problem 2; statement and original image adapted from ZeroJudge c462, “Alternating Strings”.

Problem page help

Keyboard shortcuts

Main features

  • Sample tests — Runs the sample cases bundled with the problem and auto-compares against the expected output.
  • Custom test — Run your code with your own stdin. Optionally tick the "Compare with expected (diff)" box to verify against expected output line-by-line.
  • Template — Paste the default code template you set on your profile page.
  • Collab — Edit this problem together with classmates in real time.
  • Auto-draft — Editor contents auto-save to your browser every 1.5 seconds (per account / problem / language).
  • Submit — Send your code to the judge for grading; returns AC / WA / TLE etc.

Limits

  • Source code: at most 65,536 characters
  • Custom test stdin and expected output: at most 1 MB each (≈1 million characters)
  • Custom test and sample test share the sandbox; about 1 request per 3 s per user (sample test: 1 per 1 s)
  • Custom test and sample test both have a 15 second wall-clock cap (the official judge still uses the problem time limit)
  • Interactive problems do not offer custom test (cannot simulate interaction with the judge).
  • Submitting has no rate limit, but rapid repeated submissions on the same problem are treated as score farming.