> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Repeated Substring Pattern Python Solution

> Tested Python solution for LeetCode 459 with 28 pytest cases. Generate a practice environment with lcpy.

LeetCode 459, [Easy](/catalog/easy). Topics: [String](/catalog/topics/string), [String Matching](/catalog/topics/string-matching). [View on LeetCode](https://leetcode.com/problems/repeated-substring-pattern/description/).

Generate this problem as a practice environment: tested reference solution, 28 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 459   # by problem number
lcpy gen -s repeated_substring_pattern   # by problem name
```

## Problem

Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.

### Examples

```
Input: s = "abab"
Output: true
Explanation: It is the substring "ab" twice.
```

```
Input: s = "aba"
Output: false
```

```
Input: s = "abcabcabcabc"
Output: true
Explanation: It is the substring "abc" four times or the substring "abcabc" twice.
```

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/repeated_substring_pattern/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/repeated_substring_pattern/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def repeated_substring_pattern(self, s: str) -> bool:
        n = len(s)
        lps = [0] * n
        length = 0
        for i in range(1, n):
            while length > 0 and s[i] != s[length]:
                length = lps[length - 1]
            if s[i] == s[length]:
                length += 1
            lps[i] = length
        longest_proper_suffix = lps[n - 1] if n > 0 else 0
        return longest_proper_suffix > 0 and n % (n - longest_proper_suffix) == 0
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.