| 1 | Build LPS array | - | - | - | Compute LPS for pattern "ABABCABAB" | - | LPS: [0,0,1,2,0,1,2,3,4] |
| 2 | Compare text[i] and pattern[j] | 0 | 0 | - | Match 'A' == 'A', advance i and j | - | Text: A B A B D ...
Pattern: A B A B C ...
i=1, j=1 |
| 3 | Compare text[i] and pattern[j] | 1 | 1 | - | Match 'B' == 'B', advance i and j | - | i=2, j=2 |
| 4 | Compare text[i] and pattern[j] | 2 | 2 | - | Match 'A' == 'A', advance i and j | - | i=3, j=3 |
| 5 | Compare text[i] and pattern[j] | 3 | 3 | - | Match 'B' == 'B', advance i and j | - | i=4, j=4 |
| 6 | Compare text[i] and pattern[j] | 4 | 4 | - | Mismatch 'D' != 'C', use LPS[j-1]=LPS[3]=2 to shift pattern | - | i=4, j=2 |
| 7 | Compare text[i] and pattern[j] | 4 | 2 | - | Mismatch 'D' != 'A', use LPS[j-1]=LPS[1]=0 to shift pattern | - | i=4, j=0 |
| 8 | Compare text[i] and pattern[j] | 4 | 0 | - | Mismatch 'D' != 'A', advance i | - | i=5, j=0 |
| 9 | Compare text[i] and pattern[j] | 5 | 0 | - | Match 'A' == 'A', advance i and j | - | i=6, j=1 |
| 10 | Compare text[i] and pattern[j] | 6 | 1 | - | Match 'B' == 'B', advance i and j | - | i=7, j=2 |
| 11 | Compare text[i] and pattern[j] | 7 | 2 | - | Match 'A' == 'A', advance i and j | - | i=8, j=3 |
| 12 | Compare text[i] and pattern[j] | 8 | 3 | - | Mismatch 'C' != 'B', use LPS[j-1]=LPS[2]=1 to shift pattern | - | i=8, j=1 |
| 13 | Compare text[i] and pattern[j] | 8 | 1 | - | Mismatch 'C' != 'B', use LPS[j-1]=LPS[0]=0 to shift pattern | - | i=8, j=0 |
| 14 | Compare text[i] and pattern[j] | 8 | 0 | - | Mismatch 'C' != 'A', advance i | - | i=9, j=0 |
| 15 | Compare text[i] and pattern[j] | 9 | 0 | - | Mismatch 'D' != 'A', advance i | - | i=10, j=0 |
| 16 | Compare text[i] and pattern[j] | 10 | 0 | - | Match 'A' == 'A', advance i and j | - | i=11, j=1 |
| 17 | Compare text[i] and pattern[j] | 11 | 1 | - | Match 'B' == 'B', advance i and j | - | i=12, j=2 |
| 18 | Compare text[i] and pattern[j] | 12 | 2 | - | Match 'A' == 'A', advance i and j | - | i=13, j=3 |
| 19 | Compare text[i] and pattern[j] | 13 | 3 | - | Match 'B' == 'B', advance i and j | - | i=14, j=4 |
| 20 | Compare text[i] and pattern[j] | 14 | 4 | - | Match 'C' == 'C', advance i and j | - | i=15, j=5 |
| 21 | Compare text[i] and pattern[j] | 15 | 5 | - | Match 'A' == 'A', advance i and j | - | i=16, j=6 |
| 22 | Compare text[i] and pattern[j] | 16 | 6 | - | Match 'B' == 'B', advance i and j | - | i=17, j=7 |
| 23 | Compare text[i] and pattern[j] | 17 | 7 | - | Match 'A' == 'A', advance i and j | - | i=18, j=8 |
| 24 | Compare text[i] and pattern[j] | 18 | 8 | - | Match 'B' == 'B', pattern fully matched | Pattern found at index 10 | Match at i=18, j=8 |
| 25 | End | 18 | 8 | - | Pattern found, stop | Pattern found at index 10 | Search complete |