Practice problem · easy
A monitoring system writes one letter every minute to describe a service: a for healthy, b for slow, c for down. A service that restarts in a loop produces a log made of one short block of letters, repeated again and again.
Your team lead asks: "Flag a service whose whole log is one block repeated at least twice."
Write is_repeated_block(log).
true if some block of letters, written two or more times one after another, makes exactly log.false otherwise. A log that is a single letter is never a repeat, because the block would have to appear twice.Input: log = "abcabcabc"
Output: trueThe log has 9 letters. A block of length k can fill it only when k divides 9, so the only candidates are 1 and 3.
| Block length k | Does k divide 9? | Does every letter equal the letter k places earlier? | Result |
|---|---|---|---|
| 1 | yes | no: b is not a at position 1 |
not a block |
| 2 | no | not checked | skipped |
| 3 | yes | yes: every letter matches the one 3 places earlier | the block abc fits |
Block length 3 fits, so the answer is true.
Input: log = "abcabcab"
Output: falseThe log has 8 letters, so only the block lengths 1, 2 and 4 can divide it. None of them matches: the pattern abc has period 3, and 3 does not divide 8.
Input: log = "aaaa"
Output: trueThe block a repeated 4 times, and the block aa repeated twice, both work.
1 ≤ len(log) ≤ 100,000log uses only the lowercase letters a to z.A log can have 100,000 letters, and a block can have any length up to half of that, which is 50,000 lengths. Reading the log for each of them costs billions of steps, up to 5 billion. But a block length that does not divide 100,000 can be skipped without reading anything, and 100,000 has only 36 divisors.
is_repeated_block · runs in your browserCoding is easiest on a laptop. On a phone, read, trace and try small edits.