Code RoomSmallest string period
MediumPrep Room Coding #627

Smallest string period

CodingAlgorithms & data structuresMid–Senior~25 min

The smallest period of a string s is the length of the shortest prefix p such that s is formed by repeating p a whole number of times (so "abcabcabc" has smallest period 3, "aaaa" has 1, and "abcd" has 4 because it does not repeat). Return that period length for the given s; for the empty string return 0. The string can be up to 10^5 characters, so brute-forcing every candidate period and verifying it character-by-character may be too slow.

Implement
smallest_period(s: str) → int
Examples
in["abcabcabc"]out3
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 25 min
InputExpectedGot
["abcabcabc"]3not run yetsample