This is related to necklace polynomials.
Let $a_{kn}$ count the number of aperiodic strings of $n$ letters from an alphabet of $k$ letters. For each $d \mid N$, an aperiodic string of length $d$ gives rise to a string of length $N$ with period $d$, so the total number $k^N$ of strings of length $N$ with arbitrary period can be written as
$$ k^N = \sum_{d\mid N}a_{kd}\;. $$
Then Möbius inversion yields
$$ a_{kN} = \sum_{d\mid N}\mu\left(\frac Nd\right)k^d\;. $$