| 1 | Build LPS array start | 0 | - | [0, 0, 0, 0, 0] | Initialize lps and pointers | Pattern: A B C A B
LPS: 0 0 0 0 0 |
| 2 | Compare pattern[1] and pattern[0] | 1 | - | [0, 0, 0, 0, 0] | No match, length=0, lps[1]=0, i=2 | LPS: 0 0 0 0 0 |
| 3 | Compare pattern[2] and pattern[0] | 2 | - | [0, 0, 0, 0, 0] | No match, length=0, lps[2]=0, i=3 | LPS: 0 0 0 0 0 |
| 4 | Compare pattern[3] and pattern[0] | 3 | - | [0, 0, 0, 1, 0] | Match, length=1, lps[3]=1, i=4 | LPS: 0 0 0 1 0 |
| 5 | Compare pattern[4] and pattern[1] | 4 | - | [0, 0, 0, 1, 2] | Match, length=2, lps[4]=2, i=5 | LPS: 0 0 0 1 2 |
| 6 | Start matching text | 0 | 0 | [0, 0, 0, 1, 2] | Compare text[0] and pattern[0] | Text: A B C A B C A B
Pattern: A B C A B |
| 7 | Match text[0] and pattern[0] | 1 | 1 | [0, 0, 0, 1, 2] | Characters match, advance both | Match so far: A |
| 8 | Match text[1] and pattern[1] | 2 | 2 | [0, 0, 0, 1, 2] | Characters match, advance both | Match so far: A B |
| 9 | Match text[2] and pattern[2] | 3 | 3 | [0, 0, 0, 1, 2] | Characters match, advance both | Match so far: A B C |
| 10 | Match text[3] and pattern[3] | 4 | 4 | [0, 0, 0, 1, 2] | Characters match, advance both | Match so far: A B C A |
| 11 | Match text[4] and pattern[4] | 5 | 5 | [0, 0, 0, 1, 2] | Full pattern matched at index 0 | Pattern found at text index 0 |
| 12 | Continue matching | 2 | 6 | [0, 0, 0, 1, 2] | Mismatch at text[6], pattern[5]; shift pattern using LPS | Shift pattern index to 2 |
| 13 | Match text[6] and pattern[2] | 3 | 6 | [0, 0, 0, 1, 2] | Characters match, advance both | Match so far: C |
| 14 | Match text[7] and pattern[3] | 4 | 7 | [0, 0, 0, 1, 2] | Characters match, advance both | Match so far: C A |
| 15 | Match text[8] and pattern[4] | 5 | 8 | [0, 0, 0, 1, 2] | Full pattern matched at index 3 | Pattern found at text index 3 |
| 16 | End | - | 9 | [0, 0, 0, 1, 2] | Text fully scanned | Matching complete |