This question evaluates proficiency in string processing, frequency-vector reasoning, and prefix-based partitioning to determine equal-frequency contiguous blocks.
Given a string S of length n (e.g., uppercase letters), for every prefix P = S[0..i] (for i = 0..n-1), compute the maximum integer k such that:
|P|
is divisible by
k
.
P
can be partitioned into
k
contiguous
blocks of equal length
L = |P|/k
.
c
, the number of occurrences of
c
is the
same in every block
.
Equivalently, all k blocks must have identical character-frequency vectors (so each block is an anagram of the others).
Task: Output an array ans[1..n] where ans[i] is the maximum k for the prefix of length i.
S
n
integers, where the
i
-th integer is the answer for prefix
S[0..i-1]
.
S = "ABBA"
"A"
→
k=1
"AB"
→ cannot split into 2 equal-frequency blocks (
"A"
vs
"B"
), so
k=1
"ABB"
→
k=1
"ABBA"
→ split into
"AB" | "BA"
, both have
{A:1,B:1}
, so
k=2
Output: 1 1 1 2.