Local periods
Take a non-empty string s. The local period of s at index i is the smallest positive integer n such that for each 0 ≤ k < n, we have s[i+k] = s[i-n+k] whenever both sides are defined. Alternatively, it is the minimal length of a nonempty string w such that if the concatenation w w is placed next to s so that the second copy of w begins at index i of s, then the two strings agree wherever they overlap.
As an example, let's compute the local period of s = "abaabbab" at (0-based) index 2.
- Try n = 1: then s[2+0] ≠ s[2-1+0], so this choice is not correct.
- Try n = 2: then s[2+0] = s[2-2+0] but s[2+1] ≠ s[2-2+1], so this is also not correct.
- Try n = 3: then s[2+0-3] is not defined, s[2+1] = s[2-3+1] and s[2+2] = s[2-3+2]. Thus the local period is 3.
Here is a visualization of the local periods using the second definition, with semicolons added between the two copies of w for clarity:
index a b a a b b a b period
0 a;a 1
1 b a;b a 2
2 a a b;a a b 3
3 a;a 1
4 b b a b a a;b b a b a a 6
5 b;b 1
6 a b b;a b b 3
7 b a;b a 2
Note that w is not necessarily a substring of s. This happens here in the index-4 case.
The task
Your input is a nonempty string s of lowercase ASCII characters. It can be taken as a list of characters if desired. Your output shall be the list containing the local period of s at each of its indices. In the above example, the correct output would be [1,2,3,1,6,1,3,2].
The lowest byte count in each language wins. Standard code-golf rules apply.
Test cases
a -> [1]
hi -> [1, 2]
www -> [1, 1, 1]
xcxccxc -> [1, 2, 2, 5, 1, 3, 2]
abcbacb -> [1, 4, 7, 7, 7, 3, 3]
nininini -> [1, 2, 2, 2, 2, 2, 2, 2]
abaabbab -> [1, 2, 3, 1, 6, 1, 3, 2]
woppwoppw -> [1, 4, 4, 1, 4, 4, 4, 1, 4]
qwertyuiop -> [1, 10, 10, 10, 10, 10, 10, 10, 10, 10]
deededeededede -> [1, 3, 1, 5, 2, 2, 5, 1, 12, 2, 2, 2, 2, 2]
abababcabababcababcabababcaba -> [1, 2, 2, 2, 2, 7, 7, 7, 7, 2, 2, 2, 19, 19, 5, 5, 2, 5, 5, 12, 12, 2, 2, 2, 7, 7, 5, 5, 2]
qwertyuiop
, w will be a rotated version ofqwertyuiop
. See also the example at index 4: w is not necessarily a substring of s. \$\endgroup\$;
is in your example). That would get rid of the leading 1. \$\endgroup\$