First, we build the KMP table.

- Roughly speaking, dp[i+1] stores the maximum number of characters that the string is repeating itself up to position i.
- Therefore, if a string repeats a length 5 substring 4 times, then the last entry would be of value 15.
- To check if the string is repeating itself, we just need the last entry to be non-zero and str.size() to divide (str.size()-last entry).

```
bool repeatedSubstringPattern(string str) {
int i = 1, j = 0, n = str.size();
vector<int> dp(n+1,0);
while( i < str.size() ){
if( str[i] == str[j] ) dp[++i]=++j;
else if( j == 0 ) i++;
else j = dp[j];
}
return dp[n]&&dp[n]%(n-dp[n])==0;
}
```