All problems
0629EasyStringString MatchingZ AlgorithmKnuth–Morris–Pratt Algorithm

Ribbon Repeats a Motif

Tracked in this browser only
Write code

Trains the technique from

LeetCode 459Repeated Substring Pattern

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A printed ribbon is described by the lowercase string s, one letter per printed cell, read from the left end to the right end.

Call the ribbon patterned when there is a block of cells strictly shorter than the whole ribbon such that laying that block down twice or more, end to end and always in the same direction, reproduces s exactly. Return true when the ribbon is patterned and false when it is not.

Examples

Example 1

Input
s = "abcabcabc"
Output
true

The block abc is shorter than the ribbon, and laying it down three times spells out the ribbon exactly, so the answer is true.

Example 2

Input
s = "aab"
Output
false

A block would have to divide the length of 3, so the only candidate shorter than the ribbon has length 1. Neither a nor b repeated three times spells aab, so the answer is false.

Example 3

Input
s = "abaababaab"
Output
true

The block abaab laid down twice spells abaababaab, which is the ribbon, so the answer is true.

Constraints

  • 1 <= s.length <= 10^4
  • s consists of lowercase English letters.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def repeated_substring_pattern(s: str) -> bool:
Java
public boolean repeatedSubstringPattern(String s)
September 7
Apply