I think you can handle the problem, as indicated, for arbitrary alphabet sizes, given floating point precision. For an alphabet of n characters, map the text characters to the (complex) nth roots of one. For pattern characters, map # to 0 and match regular characters with the multiplicative inverse of the corresponding text, as well as the nth roots of one.
Then you use the convolution theorem to determine at each point a point product of the text from that point with the pattern. If the text and the pattern match, each component of the product will be either 0 (on the wild card) or 1, from r * r ^ -1, so if you have a match, the result will be m, where m is the number of asymmetric characters in the pattern. If there is no coincidence, then not all components of the point product will be 1. Considering these complex numbers as two-dimensional vectors, the point product of these vectors with a vector representing 1 will be less than m, therefore, the mismatch cannot cause the result m and look like a match.
I note that if you divide the text into buffers several times by the length of the template, you can use the FFT of this length quite efficiently, so the complexity is not n log n, where n is the length of the text to be searched, but n log m, where m - the length of the template. Despite this, I will need to see the control points before I believe that this is a competitive method, compared even with a naive search.
source share