The Fischer-Paterson method computes all Hamming distances full
This proof shows: correctness.
Indices are 0-based, 1≤m≤n, and h[i]=∣{r∈{0,…,m−1}:P[r]=T[i+r]}∣ for 0≤i≤n−m. Let S be the set of distinct symbols of P. For c∈S, xc[q]=[P[m−1−q]=c] for 0≤q<m, and for a block start b, yc,b[j]=[T[b+j]=c] for 0≤j<ℓb, where ℓb=min(2m−1,n−b). All other entries of the vectors of length N are 0, and N≥3m−2.
Lemma 1 (one block, one symbol). Let z=xc∗yc,b be the linear convolution, z[s]=∑q+j=sxc[q]yc,b[j]. For each t with 0≤t≤m−1 and b+t≤n−m,
z[t+m−1]=∣{r∈{0,…,m−1}:P[r]=c and T[b+t+r]=c}∣. Proof. z[t+m−1]=∑q=0m−1xc[q]yc,b[t+m−1−q], since xc[q]=0 for q≥m and the index j=t+m−1−q is at least t≥0. Substitute r=m−1−q, which runs over 0,…,m−1: the term is [P[r]=c]⋅yc,b[t+r]. Here t+r≤2m−2<2m−1 and b+t+r≤(n−m)+(m−1)=n−1, so t+r<ℓb and yc,b[t+r]=[T[b+t+r]=c]. The sum of the products of the two indicators counts the stated set. □
Lemma 2 (the transform is exact). Line L6 returns z exactly.
Proof. xc has nonzero entries only below m and yc,b only below 2m−1, so z[s]=0 for s>3m−3, and the linear convolution has length at most 3m−2≤N. The pointwise product of the transforms of length N gives the cyclic convolution z~[s]=∑q+j≡s(modN)xc[q]yc,b[j]. A pair with q+j≥N would need q+j≥3m−2, but q+j≤(m−1)+(2m−2)=3m−3. So z~=z. Over the integers modulo the prime p=998244353, the number-theoretic transform satisfies the same convolution theorem, because N is a power of two that divides p−1, so a primitive N-th root of unity exists. So the result is z[s]modp. Each z[s] is a count of at most m pairs, and 0≤z[s]≤m<p, so z[s]modp=z[s]. □
Lemma 3 (the blocks). Each i∈{0,…,n−m} receives a contribution in line L7 from exactly one block per symbol, the one with b=m⌊i/m⌋, at t=i−b.
Proof. The blocks have b=0,m,2m,… with b≤n−m. Line L7 of block b adds to A[b+t] for 0≤t≤min(m,n−m+1−b)−1, that is, for the starts b,…,min(b+m−1,n−m). These ranges are disjoint for different b, since each has length at most m and they start m apart, and their union is {0,…,n−m}, since the last block has b>n−2m and its range ends at n−m. The block that contains i is the one with b≤i<b+m, that is, b=m⌊i/m⌋, and this b≤i≤n−m is one of the blocks of line L4. □
Theorem. FP-HAMMING returns h.
Proof. By Lemmas 1 to 3, after the loops,
A[i]=c∈S∑∣{r:P[r]=c and T[i+r]=c}∣. For each r, the condition P[r]=c=T[i+r] holds for at most one c, namely c=P[r]∈S, and it holds for that c exactly when P[r]=T[i+r]. So the sets in the sum are disjoint, and their union is {r:P[r]=T[i+r]}. So A[i]=m−h[i], and line L8 returns h[i]=m−A[i]. ■