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

&nbsp;
Example 1:

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


Example 2:

Input: s = "aba"
Output: false


Example 3:

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


&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 104
	s consists of lowercase English letters.


class Solution:
    def repeatedSubstringPattern(self, s: str) -> bool:
        for i in range(1, len(s)//2+1):
            if s[:i] * (len(s)//i) == s:
                return True
        return False
class Solution {

  public boolean repeatedSubstringPattern(String s) {
       String temp="";
        for(int i=0 ;i<s.length()/2 ;i++){
            temp+=s.charAt(i);

            if(s.length()%temp.length()==0) {
                int times_repeat= s.length()/temp.length();
                StringBuilder str = new StringBuilder();
                for(int j=0 ;j<times_repeat ;j++){
                    str.append(temp);
                }
                if(str.toString().equals(s)) return true;
            }
        }
        return false;
    }
}
class Solution {
public:
    
    vector<int> get_kmp_table(const string &s) {
        vector<int> table(s.size());
        int i=0; int j=-1;
        table[0] = -1;
        while (i < s.size()) {
            if (j == -1 || s[i] == s[j]) {
                i++;
                j++;
                table[i] = j;
            }
            else {
                j = table[j];
            }
        }
        return table;
    }
    
    bool validate_table(const vector<int>& table) {
        int idx = table.size() - 1;
        while (idx >= 0 && table[idx] > 0) {
            idx--;
        }
        if (idx <= 0) return false;
        int substr_len = idx;
        if (table.size() % substr_len != 0) return false;
        idx = idx + 1; // the first nonzero element in the string
        while (idx < table.size()-1) {
            if (table[idx] != table[idx+1]-1) return false;
            idx++;
        }
        return true;
    }
    
    bool repeatedSubstringPattern(string s) {
        if (s.size() <= 1) return true;
        
        auto table1 = get_kmp_table(s);
        string ss = s;
        reverse(ss.begin(), ss.end());
        auto table2 = get_kmp_table(ss);
        
        return (validate_table(table1) && validate_table(table2));
        
    }
};
var repeatedSubstringPattern = function(s) {
    let repeatStr = s.repeat(2) //first duplicate the string with repeat function
    let sliceStr = repeatStr.slice(1,-1) // slice string first and last string word
    return sliceStr.includes(s) // now check if the main string(s) is included by sliced string
}
Repeated Substring Pattern