← Quiz: String Matching — KMP and Rabin-Karp: all 10 questions
Quiz: String Matching — KMP and Rabin-Karp · Question 5 of 10
Why is KMP search O(n + m)?
Why is KMP search O(n + m)?
- A.It skips half the text
- B.The text index only moves forward, and j can fall back at most as many times as it went up
- C.It sorts the text first
- D.It uses hashing
Answer & explanation
Correct answer: The text index only moves forward, and j can fall back at most as many times as it went up
j increases by at most 1 per text letter, so total fall-backs are at most n.
Can you get the rest right without peeking?
Take the timed Quiz: String Matching — KMP and Rabin-Karp quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →