The frequency signature idea clicked pretty quickly but I kept second-guessing myself on the edge cases.
Use prefix frequency vectors and check divisibility: for each prefix length i, if n % i != 0, output 1; otherwise, compare the frequency vector of the first i characters with the frequency vector of each subsequent block of length i. To optimize, precompute prefix frequency arrays or use rolling hashes of frequency vectors to enable O(1) block comparisons.
Pro tip: Mention that you can precompute a rolling hash of the frequency vector for each prefix to compare blocks in O(1) time, reducing the overall complexity to O(n log n) or O(n sqrt n) depending on implementation. Also, note that you only need to check prefix lengths that divide n.
Clarify that for each prefix length i (1 to n), you must determine if the string can be partitioned into blocks of length i, each having the same frequency vector as the prefix of length i. Note that only lengths dividing n can be valid.
Compute the frequency vector of the entire string and of each prefix. This can be done by maintaining a running count of each letter as you iterate through the string.
For each i from 1 to n, if n % i != 0, mark as invalid. Otherwise, compare the frequency vector of the first i characters with the frequency vector of each subsequent block of length i. If all match, mark valid.
To avoid O(n^2) comparisons, use a rolling hash of frequency vectors or precompute prefix frequency arrays to compare blocks in O(1). Alternatively, break early if a mismatch is found.
Return an array of 0s and 1s for each prefix length. Discuss time and space complexity, and possible optimizations for large n.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.