Knuth Morris Pratt
Last updated
What is Knuth Morris Pratt?
Knuth morris pratt is a string searching algorithm that uses a precomputed array to find the substring in a string. This array is known as the prefix function. The prefix function is the longest prefix that is also a suffix of a substring. The prefix function is used to skip the characters that are already matched. The algorithm is as follows:
- Compute the prefix function of the substring.
- Traverse through the string and substring simultaneously.
- If the characters match, increment the index of both the string and substring.
- If the characters don't match, increment the index of the string by the value of the prefix function at the index of the substring.
Free resources to learn Knuth Morris Pratt
More in stage 11: Design Patterns
Knuth Morris Pratt: common questions
Can I learn Knuth Morris Pratt in Hindi?
This lesson is written in English. Once you sign in, open it in the interactive course and ask the AI tutor to explain Knuth Morris Pratt in Hindi or Hinglish. The tutor answers from this lesson’s own text, so it stays on the syllabus, and it runs on your own Google, OpenAI or Anthropic key.
Is this Knuth Morris Pratt tutorial for beginners?
It is lesson 93 of 188 in the free Computer Science roadmap, in stage 11, Design Patterns. If you are new to Computer Science, start from stage 1 and come back here in order; if you already know the earlier stages, this lesson stands on its own.