• String
  • Substring
  • Search

Sunday designed an algorithm where the pattern characters are scanned from the least frequent one to the most frequent one. Doing so one may hope to have a mismatch most of the times and thus to scan the whole text very quickly. One needs to know the frequencies of each of the character of the alphabet.

The preprocessing phase of the Optimal Mismatch algorithm consists in sorting the pattern characters in decreasing order of their frequencies and then in building the Quick Search bad-character shift function (see chapter Quick Search algorithm) and a good-suffix shift function adapted to the scanning order of the pattern characters. It can be done in O(m2+sigma) time and O(m+sigma) space complexity.

The searching phase of the Optimal Mismatch algorithm has a O(mn) time complexity.

Main features:

  • variant of the Quick Search algorithm;
  • requires the frequencies of the characters;
  • preprocessing phase in O(m2+sigma) time and O(m+sigma) space complexity.
  • searching phase in O(mn) time complexity.


Preprocessing phase

Optimal Mismatch algorithm tables
Tables used by Optimal Mismatch algorithm.

Searching phase

First attempt
 1     2 

Shift by: 3 (adaptedGs[1])

Second attempt
 1 4 3 2 

Shift by: 2 (qsBc[A]=adaptedGs[3])

Third attempt

Shift by: 9 (qsBc[T])

Fourth attempt

Shift by: 7 (qsBc[C])

The Optimal Mismatch algorithm performs 15 character comparisons on the example.