You are given two strings — text t and pattern p. You need to find index i such that t[i...i + p.size() - 1] and p have the largest "match value". Match value of two strings of equal length a and b is total number of such j that a[j] = b[j].
Can you do this faster than O(n2) (tricks with bitsets do not count)?
Is the alphabet small?
One can do it in
O(A * N * log N)
time. For a fixed letter, we need to compute dot products of the corresponding bit vectors (where 1 stands for this letter and 0 stands for the rest) for all values ofi
. We can do it efficiently be reversing the bit vector fort
and multiplying two bit vectors as polynomials using FFT.Once we know how to compute the answer for one letter, we just need to find the sum over all letters in the alphabet.
Here's a problem that asks to do something like this: Fuzzy Search.
Thanks, I didn't think of using FFT here.
Is the alphabet small?
— yeah, during thinking about the problem I also assumed that we have small alphabet. I wonder if there is a solution without this assumption.