Watch KMP build the LPS table, then match the pattern without stepping back in the text. Worked example finding AABAA in CDAABAABAAB, or type your own strings.
About this algorithm
KMP finds every place a pattern occurs in a text in O(N + M) time, where N is the length of the text and M the length of the pattern. Before searching, it studies the pattern to build an LPS table. After a mismatch, the table says how much of the match so far can be kept, so the position in the text never moves backwards.
Use KMP when you search for one pattern and need linear time guaranteed, especially on repetitive text such as DNA or log lines, where the simple method of trying every start position can take O(N × M). The LPS table also solves problems of its own: the shortest repeating unit of a string, Repeated Substring Pattern and Shortest Palindrome. Rabin-Karp hashes each window instead and extends easily to several patterns of one length, but collisions can slow it down. For many patterns at once, Aho-Corasick builds KMP's fallback idea into a trie.
For each position k of the pattern, lps[k] is the length of the longest proper prefix of pattern[0..k] that is also a suffix of it, where proper means shorter than the whole piece. This page shows the table already built when the search begins.
Keep i in the text and j in the pattern, both from 0. When text[i] equals pattern[j], move both forward.
If j is above 0, set j to lps[j - 1] and compare the same text character again: that many pattern characters still line up with the text, because they are a suffix of what just matched. If j is 0, move i forward instead.
When j reaches M, a match starts at i - M. Set j to lps[M - 1] so that overlapping matches are found as well.
The page loads the text CDAABAABAAB and the pattern AABAA. The LPS table is [0, 1, 0, 1, 2]: the last entry is 2 because AABAA ends with AA, which is also how it begins. C and D each mismatch the first A, so i moves on while j stays at 0. From index 2 all five characters match, giving the first match at index 2. Instead of starting over, j drops to lps[4] = 2, because the AA that ended this match can begin the next one. Comparing B, A and A from index 7 completes a second match at index 5, overlapping the first. One last B is compared and the text runs out. KMP makes 11 comparisons here; trying each of the 7 start positions from scratch takes 17.
The LPS table is built by running the search idea on the pattern against itself. Keep len, the length of the current matching prefix. If pattern[i] equals pattern[len], set lps[i] to len + 1 and move on; on a mismatch with len above 0, fall back to lps[len - 1] and try again; with len at 0, set lps[i] to 0. For AABAA this gives 0, 1, 0, 1, 2, and like the search it takes at most about 2M steps.
Time: the table takes O(M) to build, and in the search every step either moves i forward or moves j back, and j can only fall back as far as it has advanced, so the search takes at most 2N steps. Space: the LPS table holds one number per pattern character, and the text is read in place.