Recall & Review
beginner Click to reveal answer
What is the main goal of substring search in strings?
To find if a smaller string (pattern) exists inside a larger string (text), and if so, where it starts.
beginner Click to reveal answer
Explain the naive substring search approach.
Check every position in the text to see if the pattern matches starting there. Simple but can be slow for large texts.
intermediate Click to reveal answer
What is the key idea behind the Knuth-Morris-Pratt (KMP) algorithm?
Use information from previous matches to avoid re-checking characters, by building a prefix table that tells where to continue after a mismatch.
intermediate Click to reveal answer
What does the prefix table (also called lps array) represent in KMP?
For each position in the pattern, it stores the length of the longest prefix that is also a suffix up to that position.
intermediate Click to reveal answer
Why is KMP more efficient than naive substring search?
Because it never moves backward in the text, it uses the prefix table to skip unnecessary comparisons, making it run in linear time.
What does substring search try to find?
✗ Incorrect
Substring search looks for a smaller string (pattern) inside a bigger string (text).
In naive substring search, what do we do at each position in the text?
✗ Incorrect
Naive search checks if the pattern matches starting at each position in the text.
What does the prefix table in KMP help us avoid?
✗ Incorrect
The prefix table helps KMP avoid re-checking characters that we already know match.
What is stored in the prefix table (lps array) in KMP?
✗ Incorrect
The prefix table stores the longest prefix which is also suffix length for each position in the pattern.
Why is KMP faster than naive search?
✗ Incorrect
KMP uses the prefix table to skip unnecessary comparisons, making it faster.
Describe how the naive substring search algorithm works step-by-step.
Think about sliding the pattern over the text one by one.
You got /4 concepts.
Explain the purpose and construction of the prefix table in the KMP algorithm.
Focus on how the pattern itself helps avoid re-checking.
You got /4 concepts.
