# substrings – Find the shortest prefix to generate the original string by overlay

Given a string $$S$$, I want to find the prefix string $$P$$ the shortest length, such as the original string $$S$$ can be generated by concatenating copies of $$P$$ (where overlap is allowed).

For example, if $$S = atgatgatatgat$$, I want to find $$P = atgat$$; $$P$$ is the smallest prefix of $$S$$ which can be concatenated (in this case three times, starting with the indices $${0,3,8 }$$ of $$S$$, where the first and second copies overlap but the second and third copies do not overlap) $$S$$.

Obviously, there is a $$mathcal {O} (n ^ 2)$$ algorithm by checking each prefix of $$S$$but a colleague mentioned that it might be possible to do it $$mathcal {O} (n log n)$$. I'm thinking of using suffix tables for different prefixes of $$S$$ but have not quite been able to continue from there.