Data Structures & Algorithms

Knuth Morris Pratt Pattern Search Algorithm

// SOLVING THIS WITH AN AI ASSISTANT (2026)

If you are working through this problem with an AI coding assistant — Claude, ChatGPT, Cursor chat, Gemini, GitHub Copilot, Aider, or any agent — the goal isn’t to ask for the answer. It is to use the tool to understand the pattern. The prompt sequence I’d run:

  1. Spec it back to me first. “In your own words, what is this problem actually testing? What’s the smallest example that fails the naive approach?”
  2. Brute-force first, optimize after. “Write the simplest correct solution, even if it’s O(n²). Don’t optimize. Just make it correct, with comments explaining each step.”
  3. Ask for the upgrade. “Now show me the optimal solution. What insight makes it possible? What pattern is this an instance of?”
  4. Stress-test it. “Generate 10 edge cases — empty input, single element, duplicates, max size, sorted, reverse-sorted. Run my solution against each.”

The pattern matters more than the answer. If the agent just hands you optimized code, you’ve trained yourself to lose interviews.

What is Knuth Morris Pratt or KMP algorithm ?

KMP is an algorithm which is used in the applications of pattern matching like, find a Word ‘W’ in a String ‘S’ in O(M+N) time complexity M being the length of the string and N being the length of the word.

Here is the video explanation

In a brute force approach it would time O(M*N) time complexity with KMP it can be optimized in O(M+N).

Let’s look into how a naive brute force approach work.

Naive Approach

In the above-given string “ABCABCEE” and the word to match is “ABCE” if you look into the picture after Iteration 4, the comparison started from index 1 instead of index 4, so in a naive brute force approach every time the mismatch happened it starts comparing at the next index so the worst-case time complexity for this is O(M*N) as we are comparing the whole word “ABCE” for every letter in the given string “ABCABCEE” until we find the match.

Now lets look into how KMP works

Knuth Morris Pratt KMP

With KMP approach if you look into the iteration 5 the comparison started at Index 4 instead of starting from Index 0 like in the naive approach, which reduces the times of comparison and the total time complexity comes to O(M+N).

How to do we achieve this?

The idea is to reduce the number of backtracking when there is a letter mismatch between the given String S and Word W.

We can do this if we know if there is a prefix in W that occurs in S more than once after at least one character is found.

Algorithm:

  • Initialize an array of size word W
  • Populate the array based on the prefix index like below

Word: ABCDA

0 0 0 0 1

Word: CACBCA

0 0 0 1 2

In the above word ABCDA, letter A is already there at index 0 so when there is a mismatch we would start comparison form index 1. Similarly in the word CACBCA the prefix CA has been repeated to when there is a mismatch we would start the comparison from index 3.
This way we reduce the number of times we backtrack, thus resulting in O(M+N).

Java Solution:

First we will write a function to generate the above array

public static int[] generatePattern(String word) {
    int wordLength = word.length();
    int len = 0;
    int i = 1;
    int[] wordPatternArray = new int[wordLength];
    wordPatternArray[0] = 0;

    while (i < patternLength) {
        if (word.charAt(i) == word.charAt(len)) {
            len++;
            wordPatternArray[i] = len;
            i++;
        } else {
            if (len != 0) {
                len = wordPatternArray[len - 1];
            } else {
                wordPatternArray[i] = len;
                i++;
            }
        }
    }
    return wordPatternArray;
}

Now we will implement the KMP function

public static List<Integer> doKMPSearch(String text, String word) {
    int[] wordPatternArray = generatePattern(word);

    int textIndex = 0;
    int wordIndex = 0;

    List<Integer> foundIndexes = new ArrayList<>();

    while (textIndex < text.length()) {
        if (word.charAt(wordIndex) == text.charAt(textIndex)) {
            wordIndex++;
            textIndex++;
        }
        if (wordIndex == word.length()) {
            foundIndexes.add(textIndex - wordIndex);
            wordIndex = wordPatternArray[wordIndex - 1];
        }

        else if (textIndex < text.length() && word.charAt(wordIndex) != text.charAt(textIndex)) {
            if (wordIndex != 0)
                wordIndex = wordPatternArray[wordIndex - 1];
            else
                textIndex = textIndex + 1;
        }
    }
    return foundIndexes;
}

For the AI-native engineering side of HackerHeap — building MCP servers, comparing agents (Claude Code, Cursor, Windsurf, Codex, Gemini, Copilot), and weekly working code — see the Friday Build newsletter and the MCP archive.

rajendra

Recent Posts

HackerHeap is back: building with AI agents in 2026

HackerHeap is back: a multi-platform resource for working developers building with AI coding agents. We…

6 days ago

LeetCode Maximum Erasure Value

// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…

4 years ago

Largest Unique Number Java Solution

// BUILDING THIS WITH AN AI AGENT (2026)Whether you are using Claude Code, Cursor, Windsurf,…

5 years ago

Jump Search Algorithm In Java

// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…

6 years ago

Binary Search Algorithm In Java

// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…

6 years ago

Leetcode Integer to Roman Java Solution

// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…

6 years ago

This website uses cookies.