Though I suspect that, for long strings, a solution like the one you proposed but testing only cycles whose lengths are divisors of the input length would be faster. I'll do some benchmarking when I get a chance.
We seem to have moved off point from the original problem. It was my original post (Search for identical strings) which sent Grandfather on his quest to optimize a search for identical substrings within a string.
If I read this regex correctly it doesn't do what I originally asked for. That is, to find the longest set of identical substrings anywhere within a parent string.
If I read the regex in findCycle_2 correctly it looks for a string of characters that must begin at the start of the parent string, and this substring is immediately followed by zero or more copies of itself to the end of the parent string. My original code also used a regex
The time estimate for a search of my original data set was around 3 years! (But my data set is very large.) The time estimate was based on how long it took to search a subset of my dataset. My biggest problem appears to be with regex backtracking.
Anyway, back to findCycle_2. Using a test string of 'ABCDEFG123456ABCDEFG' findCycle_2 returns the entire 20 character string because it found the string but no copies of itself. If I force the presence of a second copy of the substring with
I get nothing. If I allow intervening trash with
I get nothing. In order for the regex to find any occurrence of the longest paired substrings within a parent string it appears that the regex should be written
And this appears to return me to the three year run.
I should note that Grandfather created a script which ran in under a minute for my entire dataset and found over 98% of the paired substrings.