# 459 Repeated Substring Pattern

### Problem:

Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.

Example 1:

```
Input: "abab"

Output: True

Explanation: It's the substring "ab" twice.
```

Example 2:

```
Input: "aba"

Output: False
```

Example 3:

```
Input: "abcabcabcabc"

Output: True

Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)
```

### Solutions:

```java
public class Solution {
    public boolean repeatedSubstringPattern(String str) {
        for (int i = 0; i < str.length() / 2; i ++) {
            int count = i + 1;
            if (str.length() % count != 0) {
                continue;
            }
            boolean same = true;
            for (int k = count; k + count <= str.length() && same; k+=count) {
                for (int j = 0; j <= i && same; j ++) {
                    if (str.charAt(j) != str.charAt(j + k)) {
                        same = false;
                    }
                }
            }
            if (same == true) {
                return true;
            }
        }
        return false;
    }
}
```

```java
public class Solution {
    public boolean repeatedSubstringPattern(String str) {
        int n = str.length();
        for (int count = n / 2; count >= 1; count --) {
            if (n % count == 0) {
                int num = n / count;
                StringBuilder sb = new StringBuilder();
                String cand = str.substring(0, count);
                for (int j = 0; j < num; j ++) {
                    sb.append(cand); 
                }
                if (sb.toString().equals(str)) 
                    return true;
            }
        }
        return false;
    }
}
```

```java
public class Solution {
    public boolean repeatedSubstringPattern(String str) {
        for (int i = 1; i <= str.length() / 2; i ++) {
            if (str.length() % i == 0) {
                if (str.substring(0, str.length() - i).equals(str.substring(i))) {
                    return true;
                }
            }
        }
        return false;
    }
}
```

```java
public class Solution {
    public boolean repeatedSubstringPattern(String s) {
        String doubled = s + s;
        int cut = doubled.indexOf(s, 1);
        if (cut >= 1 && cut < s.length()) {
            return true;
        }
        return false;
    }
}
```


---

# 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://ugopireddy.gitbook.io/leet-code-solutions/solutions-451-500/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.
