Repeated Substring Pattern

Given a string `s`, check whether it can be built by taking one of its substrings and appending copies of it together (at least two copies in total).

30:00

Repeated Substring Pattern
easy
Free written solution
Topics
Companies

Given a string s, check whether it can be built by taking one of its substrings and appending copies of it together (at least two copies in total).

Example 1:
Input: {"s":"abab"}
Output: true
Constraints:
  • 1≤s.length≤1041 \leq s.\text{length} \leq 10^4

  • s consists of lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"abab"}

s = "abab" (n = 4)

s + s ; s slides along it, one shift per step

a
b
a
b
a
b
a
b

0

1

2

3

4

5

6

7

shift -
shifts tried 0 / 3

If s is some block repeated, rotating s by one block length gives s back. Rotations of s are exactly the length-4 windows of s + s.

Variables
VariableValue
n4
DepthFunction Call
Stack empty
0/5