What does KMP stand for in the KMP Pattern Matching Algorithm?
KMP stands for Knuth-Morris-Pratt, named after the three computer scientists who developed the algorithm.
Click to reveal answer
beginner
What is the main advantage of the KMP algorithm over the naive pattern matching approach?
KMP avoids rechecking characters that have already been matched by using a precomputed prefix table, making it faster especially for large texts and patterns.
Click to reveal answer
intermediate
What is the purpose of the 'lps' (Longest Prefix Suffix) array in KMP?
The 'lps' array stores the length of the longest proper prefix of the pattern that is also a suffix for each prefix of the pattern. It helps to skip unnecessary comparisons.
Click to reveal answer
intermediate
In KMP, what happens when a mismatch occurs after some matches?
Instead of restarting from the next character in the text, KMP uses the 'lps' array to shift the pattern to the right position, avoiding rechecking matched characters.
Click to reveal answer
intermediate
What is the time complexity of the KMP Pattern Matching Algorithm?
The time complexity is O(n + m), where n is the length of the text and m is the length of the pattern, because each character is processed at most twice.
Click to reveal answer
What does the 'lps' array represent in the KMP algorithm?
ALongest prefix which is also suffix for pattern prefixes
BLength of the pattern
CPositions of matches in the text
DNumber of mismatches found
✗ Incorrect
The 'lps' array stores the longest proper prefix which is also a suffix for each prefix of the pattern.
What is the main benefit of using KMP over naive pattern matching?
AIt uses less memory
BIt finds all substrings in constant time
CIt works only on sorted text
DIt avoids rechecking matched characters after mismatch
✗ Incorrect
KMP avoids rechecking characters that have already been matched by using the 'lps' array.
If the pattern is 'ABABC', what is the lps value for the last character 'C'?
A0
B1
C2
D3
✗ Incorrect
For 'ABABC', the longest prefix which is also suffix for the full pattern is 0 because no proper prefix matches suffix ending at 'C'.
What is the worst-case time complexity of KMP algorithm?
AO(n*m)
BO(n + m)
CO(n^2)
DO(m^2)
✗ Incorrect
KMP runs in linear time O(n + m), where n is text length and m is pattern length.
When a mismatch occurs in KMP, what does the algorithm do next?
ARestart matching from the next character in text
BStop searching
CShift pattern using lps array to avoid rechecking
DReverse the pattern
✗ Incorrect
KMP uses the lps array to shift the pattern smartly to avoid rechecking matched characters.
Explain how the 'lps' array is constructed in the KMP algorithm and why it is important.
Think about how pattern prefixes relate to suffixes and how this helps avoid repeated work.
You got /3 concepts.
Describe the process of pattern matching in KMP when a mismatch occurs after some characters have matched.
Focus on how KMP uses previous knowledge to skip unnecessary checks.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the KMP Pattern Matching Algorithm?
easy
A. To find occurrences of a pattern string within a text efficiently
B. To sort an array of numbers quickly
C. To reverse a string in linear time
D. To calculate the factorial of a number
Solution
Step 1: Understand the problem KMP solves
KMP is designed to search for a pattern inside a larger text efficiently without rechecking characters unnecessarily.
Step 2: Compare with other options
Sorting, reversing strings, and factorial calculations are unrelated to pattern searching.
Final Answer:
To find occurrences of a pattern string within a text efficiently -> Option A
Quick Check:
KMP = Pattern search [OK]
Hint: KMP is about searching patterns fast in text [OK]
Common Mistakes:
Confusing KMP with sorting algorithms
Thinking KMP reverses strings
Assuming KMP calculates mathematical functions
2. Which of the following is the correct way to declare the prefix table (lps array) in Rust for KMP algorithm?
easy
A. let mut lps = Vec::new();
B. let lps = [0; pattern.len()];
C. let lps: Vec<i32> = Vec::with_capacity(pattern.len());
D. let mut lps: Vec<usize> = vec![0; pattern.len()];
Solution
Step 1: Understand prefix table initialization
The prefix table (lps) needs to be mutable and initialized with zeros for the length of the pattern.
Step 2: Check each option
let mut lps: Vec<usize> = vec![0; pattern.len()]; correctly creates a mutable vector of usize with zeros. let lps = [0; pattern.len()]; creates an array but Rust arrays need constant size. let mut lps = Vec::new(); creates an empty vector without size. let lps: Vec<i32> = Vec::with_capacity(pattern.len()); creates a vector with capacity but no elements initialized.
Final Answer:
let mut lps: Vec<usize> = vec![0; pattern.len()]; -> Option D