# KMP Algorithm Explained In Plain English

Here I am just trying to explain KMP algorithm in plain english. I will also explain worst time complexity & why it is O(m + n).

We will take two examples, one with no repeatable character in the pattern & another with repeatable characters in the pattern.

#### Pattern With Repeatable Characters:

``````Text:    aaaabaabab

Pattern: aaaaa``````
###### Longest Prefix Suffix Table:

First, we need to build a proper prefix suffix table. Let me explain the basic idea. We will take each substring of the pattern.

Here these are “a”,”aa”, “aaa”, “aaaa”. We are skipping “aaaaa” as that is the whole pattern. If we get a match for that, we are done.

For each of this substring, we will check if there are some consecutive characters which are present in the beginning & also at the end.

So why do we need that? Idea is simple. Suppose, we matched “aaaa”, then we had a mismatch. We know “aaa” is common in the beginning & at the end.

As we matched last three “a”, we know that starting three “a” are already matched. So we can skip them when we start matching again & directly go to 4th character. Let’s see below.

Here is the prefix suffix table for the example above. It’s easy to understand. All characters are same. So proper prefix suffix length will be substring length – 1.

```Index: 1 2 3 4

Value: 0 1 2 3```
###### Search Steps:

Now let’e match it.

``````t: aaaabaabab

p: aaaaa``````

t = p, match length 1, increment both t & p.

``````t: aaaabaabab

p: aaaaa``````

t = p, match length 2, increment both t & p.

``````t: aaaabaabab

p: aaaaa``````

t = p, match length 3, increment both t & p.

``````t: aaaabaabab

p: aaaaa``````

t = p, match length 4, increment both t & p.

``````t: aaaabaabab

p: aaaaa``````

t ≠ p, match failed, we won’t move t in that case, prefix = 3 from the table, so we set p to prefix + 1 = 4.

``````t: aaaabaabab

p: aaaaa``````

t ≠ p, match failed, we won’t move t in that case, prefix = 2 from the table, so we set p to prefix + 1 = 3.

``````t: aaaabaabab

p: aaaaa``````

t ≠ p, match failed, we won’t move t in that case, prefix = 1 from the table, so we set p to prefix + 1 = 2.

``````t: aaaabaabab

p: aaaaa``````

t ≠ p, match failed, we won’t move t in that case, prefix = 0 from the table, as we set p to prefix + 1 = 1.

``````t: aaaabaabab

p: aaaaa``````

t ≠ p, match failed, p is at the first character, so we can’t decrement it & increment only t.

``````t: aaaabaabab

p: aaaaa``````

t = p, match length 1, increment both t & p.

``````t: aaaabaabab

p: aaaaa``````

t = p, match length 2, increment both t & p.

``````t: aaaabaabab

p: aaaaa``````

t ≠ p, match failed, we won’t move t in that case, prefix = 1 from the table, so we set p to prefix + 1 = 2.

``````t: aaaabaabab

p: aaaaa``````

t ≠ p, match failed, we won’t move t in that case, prefix = 0 from the table, so we set p to prefix + 1 = 1.

``````t: aaaabaabab

p: aaaaa``````

t ≠ p, match failed, p is at the first character, so we can’t decrement it & increment only t.

``````t: aaaabaabab

p: aaaaa``````

t = p, match length 1, increment both t & p.

``````t: aaaabaabab

p: aaaaa``````

t ≠ p, match failed, we won’t move t in that case, prefix = 0, from the table, so we set p to prefix + 1 = 1.

``````t: aaaabaabab

p: aaaaa``````

t ≠ p, match failed, p is at the first character, so we can’t decrement it & increment only t, but t is at the last position, so matching ends here without finding the pattern.

So as you can see, t is never decremented. So if text length is n, we have a part of the complexity as n.

But p is decremented. If pattern length is m, is worst case time complexity O(mn)?

No. If you look at the above steps carefully, p was decrement 4 times consecutively after it was matched 4 times. Then p was decremented 2 times after it was matched 2 times. At last it was decremented 1 time after it was matched 1 time.

So basically to have n negative steps, you need n positive steps & n positive steps means you are done traversing through the whole text.

So worst time complexity is n + n = 2n which is O(n).

The time complexity of building proper prefix suffix table is O(m). Probably I will write a separate blog post on that. Here I want to focus on the actual search algorithm of KMP.

So total time complexity of KMP algorithm is O(m + n).

In our example above, we took a pattern with all repeatable characters. Now let’s take an example where all characters are different.

#### Pattern Without Repeatable Characters:

```Text:    aaaabaabab

Pattern: abcde```
###### Longest Prefix Suffix Table:

Prefix suffix table is simple. There is no match among the characters in the pattern. So all values are 0.

```Index: 1 2 3 4

Value: 0 0 0 0```
###### Search Steps:

Now let’s match it.

```t: aaaabaabab

p: abcde```

t = p, match length is 1, increment both t & p.

```t: aaaabaabab

p: abcde```

t ≠ p, match failed, we won’t increment t in that case, prefix = 0 from the table, so we set p to prefix + 1 = 1.

```t: aaaabaabab

p: abcde```

t = p, match length 1, increment both t & p.

```t: aaaabaabab

p: abcde```

t ≠ p, match failed, we won’t increment t in that case, prefix = 0 from the table, so we set p to prefix + 1 = 1.

```t: aaaabaabab

p: abcde```

t = p, match length 1, increment both t & p.

```t: aaaabaabab

p: abcde```

t ≠ p, match failed, we won’t increment t in that case, prefix = 0 from the table, so we set p to prefix + 1 = 1.

```t: aaaabaabab

p: abcde```

t = p, match length 1, increment both t & p.

```t: aaaabaabab

p: abcde```

t = p, match length 2, increment both t & p.

```t: aaaabaabab

p: abcde```

t ≠ p, match failed, we won’t increment t in that case, prefix = 0 from the table, so we set p to prefix + 1 = 1.

```t: aaaabaabab

p: abcde```

t = p, match length 1, increment both t & p.

```t: aaaabaabab

p: abcde```

t ≠ p, match failed, we won’t increment t in that case, prefix = 0 from the table, so we set p to prefix + 1 = 1.

```t: aaaabaabab

p: abcde```

t = p, match length 1, increment both t & p.

```t: aaaabaabab

p: abcde```

t = p, match length 2, increment both t & p.

```t: aaaabaabab

p: abcde```

t ≠ p, match failed, we won’t increment t in that case, prefix = 0 from the table, so we set p to prefix + 1 = 1.

```t: aaaabaabab

p: abcde```

t = p, match length 1, increment both t & p.

```t: aaaabaabab

p: abcde```

t = p, match length 2, increment both t & p, but t is at the last position, so matching ends here without finding the pattern.

If you notice carefully above, you can see the same thing. To decrement p, we need to match pattern characters at least same number of times beforehand.

So search time complexity is 2n which translates into O(n).

If we take prefix suffix table construction time into account, then total time complexity would be O(m+n).