1. Home
  2. Design & Analysis of Algorithms
  3. KMP String Matching

KMP String Matching

Find a pattern in a text without ever reading a character twice. The LPS table tells the pattern how far it can jump after a mismatch.

Interactive 3DAdvanced13 min readDAAUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

Find a pattern inside a text. The naive way slides the pattern one step at a time and re-reads characters. Knuth–Morris–Pratt (KMP) remembers what already matched, so the text pointer never moves backwards. Press Build LPS table, then Search.

Pseudocode

  1. i ← 0 (text); j ← 0 (pattern)
  2. while i < n:
  3. if T[i] = P[j]: i ← i + 1; j ← j + 1
  4. if j = m: match at i − m; j ← lps[j − 1]
  5. else if j > 0: j ← lps[j − 1] // slide, never move i back
  6. else: i ← i + 1
Read this model as text · 31 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. Find a pattern inside a text. The naive way slides the pattern one step at a time and re-reads characters. Knuth–Morris–Pratt (KMP) remembers what already matched, so the text pointer never moves backwards. Press Build LPS table, then Search.
  2. Slide the pattern along the text. i walks through the text and never goes back; j is the position inside the pattern.
  3. T[0] = 'A' matches P[0]. Move both pointers right.
  4. T[1] = 'B' matches P[1]. Move both pointers right.
  5. T[2] = 'A' matches P[2]. Move both pointers right.
  6. T[3] = 'B' matches P[3]. Move both pointers right.
  7. T[4] = 'D' ≠ P[4] = 'C'. The first 4 characters matched, and lps[3] = 2 says their last 2 characters already match the start of the pattern.
  8. So slide the pattern 2 places at once and continue with j = 2 — no character of the text is read twice.
  9. T[4] = 'D' ≠ P[2] = 'A'. The first 2 characters matched, and lps[1] = 0 says their last 0 characters already match the start of the pattern.
  10. So slide the pattern 2 places at once and continue with j = 0 — no character of the text is read twice.
  11. T[4] = 'D' ≠ P[0] = 'A'. Nothing matched, so move i one step right.
  12. T[5] = 'A' matches P[0]. Move both pointers right.
  13. T[6] = 'B' matches P[1]. Move both pointers right.
  14. T[7] = 'A' matches P[2]. Move both pointers right.
  15. T[8] = 'C' ≠ P[3] = 'B'. The first 3 characters matched, and lps[2] = 1 says their last 1 character already match the start of the pattern.
  16. So slide the pattern 2 places at once and continue with j = 1 — no character of the text is read twice.
  17. T[8] = 'C' ≠ P[1] = 'B'. The first 1 characters matched, and lps[0] = 0 says their last 0 characters already match the start of the pattern.
  18. So slide the pattern 1 place at once and continue with j = 0 — no character of the text is read twice.
  19. T[8] = 'C' ≠ P[0] = 'A'. Nothing matched, so move i one step right.
  20. T[9] = 'D' ≠ P[0] = 'A'. Nothing matched, so move i one step right.
  21. T[10] = 'A' matches P[0]. Move both pointers right.
  22. T[11] = 'B' matches P[1]. Move both pointers right.
  23. T[12] = 'A' matches P[2]. Move both pointers right.
  24. T[13] = 'B' matches P[3]. Move both pointers right.
  25. T[14] = 'C' matches P[4]. Move both pointers right.
  26. T[15] = 'A' matches P[5]. Move both pointers right.
  27. T[16] = 'B' matches P[6]. Move both pointers right.
  28. T[17] = 'A' matches P[7]. Move both pointers right.
  29. T[18] = 'B' matches P[8]. Move both pointers right.
  30. Match found at position 10! To look for the next (possibly overlapping) one, keep the matched border: j ← lps[8] = 4.
  31. Found at position 10. KMP made 23 character comparisons (always ≤ 2n = 38); the naive method makes 29 here and up to n × m in the worst case.

Try this in the 3D model

  • Press Build LPS table for ABABCABAB. Why is lps[8] = 4?
  • Press Search. Watch the text pointer i. Does it ever move left?
  • At the first mismatch, how many places does the pattern jump, and which lps value decides that?
  • Choose the AABA example. How does KMP find the overlapping matches?

The problem

Find every place where a pattern P (length m) occurs inside a text T (length n). Your browser does this with Ctrl + F, editors use it in search-and-replace, and biologists use it to find DNA sequences.

The naive method tries the pattern at every position. After a mismatch it shifts the pattern by one and starts comparing again from the pattern’s first character, re-reading text it has already seen. With text AAAAAAAAAB and pattern AAAAB it does nearly n × m comparisons.

The KMP idea

When a mismatch happens after j characters matched, we already know those j characters: they are P[0…j−1]. Part of them may also be the start of the pattern, and then we don’t need to compare them again.

The LPS table (longest proper prefix that is also a suffix, also called the failure function) stores this for every prefix of the pattern:

k 0 1 2 3 4 5 6 7 8
P[k] A B A B C A B A B
lps[k] 0 0 1 2 0 1 2 3 4

lps[8] = 4 because ABAB is both a prefix and a suffix of ABABCABAB.

Searching with the table

Keep two pointers: i in the text and j in the pattern.

  • Match: move both right. If j reaches m, a match ends at i, so record it and set j ← lps[m − 1] to keep looking.
  • Mismatch with j > 0: set j ← lps[j − 1]. The pattern slides forward by j − lps[j − 1] places, and i stays where it is.
  • Mismatch with j = 0: move i one step right.

Because i never moves backwards, the search takes at most about 2n comparisons. On the first example in the 3D model, KMP needs 23 comparisons where the naive method needs 29. The gap grows quickly on repetitive text.

Building the LPS table

The table is built the same way, with the pattern matched against itself. len is the length of the current border:

  • If P[i] = P[len]: the border grows, so lps[i] = len + 1.
  • Otherwise, if len > 0: fall back to a shorter border with len ← lps[len − 1], and try again.
  • Otherwise lps[i] = 0.

This also takes only O(m) time.

Code

def build_lps(p):
    lps = [0] * len(p)
    length, i = 0, 1                    # length of the current border
    while i < len(p):
        if p[i] == p[length]:
            length += 1
            lps[i] = length
            i += 1
        elif length:
            length = lps[length - 1]    # try a shorter border
        else:
            lps[i] = 0
            i += 1
    return lps

def kmp_search(text, p):
    lps, found = build_lps(p), []
    i = j = 0
    while i < len(text):
        if text[i] == p[j]:
            i += 1
            j += 1
            if j == len(p):
                found.append(i - j)
                j = lps[j - 1]          # allow overlapping matches
        elif j:
            j = lps[j - 1]              # slide the pattern, keep i
        else:
            i += 1
    return found

print(build_lps("ABABCABAB"))                      # [0, 0, 1, 2, 0, 1, 2, 3, 4]
print(kmp_search("ABABDABACDABABCABAB", "ABABCABAB"))   # [10]
print(kmp_search("AABAACAADAABAABA", "AABA"))           # [0, 9, 12]
#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<int> buildLps(const string& p) {
    vector<int> lps(p.size(), 0);
    for (size_t i = 1, len = 0; i < p.size();) {
        if (p[i] == p[len]) lps[i++] = ++len;
        else if (len) len = lps[len - 1];
        else lps[i++] = 0;
    }
    return lps;
}

vector<int> kmpSearch(const string& t, const string& p) {
    vector<int> lps = buildLps(p), found;
    for (size_t i = 0, j = 0; i < t.size();) {
        if (t[i] == p[j]) {
            i++; j++;
            if (j == p.size()) { found.push_back(i - j); j = lps[j - 1]; }
        } else if (j) j = lps[j - 1];
        else i++;
    }
    return found;
}

int main() {
    for (int pos : kmpSearch("AABAACAADAABAABA", "AABA")) cout << pos << " ";   // 0 9 12
}

KMP vs other string-matching algorithms

Algorithm Preprocessing Search (worst case) Notes
Naive none O(n · m) Simple; fine for short patterns
KMP O(m) O(n) Never re-reads the text; good for streams
Rabin–Karp O(m) O(n · m), O(n + m) on average Rolling hash; great for many patterns at once
Boyer–Moore O(m + σ) O(n · m), often sub-linear Skips ahead from the right; used by grep
Aho–Corasick O(total pattern length) O(n + matches) Many patterns at once, built on a trie

KMP’s table is really a small automaton: each state is “how much of the pattern has matched”, and that links it to finite automata.

Common mistakes

  • Moving i backwards after a mismatch. That throws away the whole point of KMP.
  • Using lps[j] instead of lps[j − 1] after a mismatch at position j.
  • Forgetting to reset j ← lps[m − 1] after a full match, which misses overlapping matches like AABA in AABAABA.
  • Counting the whole string as its own border. The prefix must be proper, shorter than the string.

Complexity at a glance

Case / operationTimeWhy
Building the LPS tableO(m)m = length of the pattern.
Searching the textO(n)At most 2n comparisons; i never moves back.
Naive search, worst caseO(n · m)Re-reads text after every mismatch.
Extra spaceO(m) for the LPS table

Quick check

Test yourself — pick an answer to see if you got it.

1. What does lps[k] store?

2. What is the LPS table of the pattern AAAA?

3. After a mismatch at pattern position j > 0, what does KMP do?

4. What is the time complexity of KMP for a text of length n and a pattern of length m?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in KMP String Matching. Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.