# 459. Repeated Substring Pattern

## Problem Statement

<br>

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

&#x20;

**Example 1:**

<pre><code><strong>Input: s = "abab"
</strong><strong>Output: true
</strong><strong>Explanation: It is the substring "ab" twice.
</strong></code></pre>

**Example 2:**

<pre><code><strong>Input: s = "aba"
</strong><strong>Output: false
</strong></code></pre>

**Example 3:**

<pre><code><strong>Input: s = "abcabcabcabc"
</strong><strong>Output: true
</strong><strong>Explanation: It is the substring "abc" four times or the substring "abcabc" twice.
</strong></code></pre>

&#x20;

**Constraints:**

* `1 <= s.length <= 104`
* `s` consists of lowercase English letters.

## Intuition

```
Approach 1:
Take substrings and repeat to check if exists

Approach 2:

s = abab
Repeat and delete first and last character
abab + abab

a)bababa(b
Now try to find abab in this 
```

### Links

<https://leetcode.com/problems/repeated-string-match/description/>

### Video Links

### Approach 1:

```
Brute
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    bool repeatedSubstringPattern(string s) {
        int n = s.size();

        for(int i=1; i<=n/2; i++){
            if(n%i == 0){
                string temp = s.substr(0,i);
                string repeat = "";

                for(int j=0; j<n/i; j++){
                    repeat += temp;
                }

                if(repeat == s)
                    return true;
            }
        }

        return false;
    }
};
```

{% endcode %}

### Approach 2:

```
Optimal trick
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    bool repeatedSubstringPattern(string s) {
        string rept = s + s;
        string monk = rept.substr(1, rept.size()-2);

        if(monk.find(s) != -1)
            return true;

        return false;
    }
};
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coding-9.gitbook.io/untitled/string/easy/459.-repeated-substring-pattern.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
