Character comparisons are at the heart of the naive string matcher. For each position in the text, up to m comparisons may be made, where m is the length of the pattern. Let’s break it down:
- If we have a text of length n and a pattern of length m, there are n - m + 1 possible starting positions in the text.
- For each starting position, the algorithm checks each character of the pattern against the corresponding character in the text.
This leads to a total of \((n - m + 1) \times m\) comparisons in the worst case.
When estimating this in Big-O notation, we focus on the term that grows the fastest as the input size increases. In this case, it simplifies to \O(nm)\, indicating that the number of comparisons grows proportional to the product of the lengths of the text and the pattern.