Problem 305004 · medium · Phase 03 Linear Management & Searching

Built From a Repeated Block

strings · periodicity · divisors

A packaging machine prints labels by repeating a short block of characters. Given a non-empty string s, return True if s can be produced by taking some block and repeating it two or more times, and False otherwise.

Examples

Input:  s = "tictictic"
Output: True
Explanation: the block "tic" repeated 3 times.

Input:  s = "tictac"
Output: False

Constraints

  • 1 <= len(s) <= 10**4
  • Target: O(n * d(n)) where d(n) is the number of divisors of n, or O(n) with the doubling trick.

Goals

  • Recognise that a period must divide the length
  • Test a candidate period by slicing and multiplying
  • Optionally use the doubled-string trick for a one-liner
Starting Python…