Skip to content
BytePatterns

String Matching Intuition

Strings: lesson 7 of 11

A mismatch already tells you where to restart.

Lesson 7 of 11 · 5 min

String Matching Intuition

Step 1 of 8

Search for ababc inside abababc, starting with the pattern aligned at index 0.

The Idea

Naive matching throws away everything it just learned: after four matching characters it slides the pattern one step and re-reads them all.

Precompute, for every prefix of the pattern, the longest proper prefix that is also a suffix. On a mismatch, fall back to that length — the text pointer never moves backwards.

Real-World Example

Network intrusion sensors scan packet streams for signatures at line rate and cannot rewind a stream that has already gone past. A failure table lets the matcher keep its partial progress and read every byte exactly once.

The Code

def failure(pat):
    table = [0] * len(pat)
    k = 0
    for i in range(1, len(pat)):
        while k and pat[i] != pat[k]:   # fall back to a shorter prefix
            k = table[k - 1]
        if pat[i] == pat[k]:
            k += 1
        table[i] = k                    # prefix that is also a suffix
    return table

print(failure("ababc"))   # [0, 0, 1, 2, 0]
print(failure("aaaa"))    # [0, 1, 2, 3]

Python

Your turn

What does this print?

def failure(pat):
  table = [0] * len(pat)
  k = 0
  for i in range(1, len(pat)):
      while k and pat[i] != pat[k]:
          k = table[k - 1]
      if pat[i] == pat[k]:
          k += 1
      table[i] = k
  return table

print(failure("abab"))

Mini quiz

1 / 3

What does entry i of the failure table hold?

New lessons land every few weeks

Leave an address and we will tell you when the next one is up. That is the only reason we will use it.

One address, stored so we can email you. Nothing else, ever.