To solve this problem there was used z-function algorithm. Which calculates for each position how many characters starting from this position matches the prefix. Algorithm works in O(n) time;

To check if there all characters are period we should check two conditions:

- the length of prefix + zfunction value must be equal to the size of str. ( it will guarantee that exactly all elements exists in period
- does characters between (i -- i+z[i]) and prefix covers all characters.

I feel that there exists more elegant check for periods by using the z-function. Any ideas about that?

```
public class Solution {
public boolean repeatedSubstringPattern(String str) {
int n = str.length();
int z[] = zFunction(str.toCharArray());
for (int i=0; i<z.length; i++) {
if (i+z[i]==n && z[i]*2>=str.length()) {
return true;
}
}
return false;
}
private int [] zFunction(char c[]) {
int n = c.length;
int z[] = new int[n];
int left = 0;
int mostRight = 0;
for (int i=1; i<n; i++) {
if (i<=mostRight) {
z[i] = Math.min(mostRight-i+1, z[i-left]);
}
while (i+z[i]<n && c[i+z[i]] == c[z[i]]) {
z[i]++;
}
if (i+z[i]-1>mostRight) {
mostRight = i+z[i]-1;
left = i;
}
}
return z;
}
}
```