A period
p
of a stringw
is any positive integerp
such thatw[i]=w[i+p]
whenever both sides of this equation are defined. Letper(w)
denote the size of the smallest period ofw
. We say that a stringw
is periodic iffper(w) <= |w|/2
.
So informally a periodic string is just a string that is made up from a prefix repeated at least twice. The only complication is that at the end of the string we don't require a full copy of the prefix.
For, example consider the string x = abcab
. per(abcab) = 3
as x[1] = x[1+3] = a
, x[2]=x[2+3] = b
and there is no smaller period. The string abcab
is therefore not periodic. However, the string ababa
is periodic as per(ababa) = 2
.
As more examples, abcabca
, ababababa
and abcabcabc
are also periodic.
Is there a regex to determine if a string is periodic or not?
I don't really mind which flavor of regex but if it makes a difference, anything that Python re
supports.