Repeated Substring Pattern
Real-world version: Ribbon Repeats a MotifThe statement here is our own: the same inputs, outputs and technique.
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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def repeated_substring_pattern(s: str) -> bool:public boolean repeatedSubstringPattern(String s)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Find the Index of the First Occurrence in a StringReal-world version: Locate the Routing TagEasy
- Repeated String MatchReal-world version: Repeats Of A Motif Holding A StretchMedium
- Find Beautiful Indices in the Given Array IReal-world version: Nearby Marker PositionsMedium
- Shortest PalindromeReal-world version: Bead Strand MirrorHard