🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: String Matching — KMP and Rabin-Karp: all 10 questions

Quiz: String Matching — KMP and Rabin-Karp · Question 1 of 10

What is the worst-case time of naive string matching for text length n and pattern length m?

What is the worst-case time of naive string matching for text length n and pattern length m?

  1. A.O(n + m)
  2. B.O(n log m)
  3. C.O(n · m)
  4. D.O(m²)
Answer & explanation

Correct answer: O(n · m)

At each of about n starting positions it may compare up to m letters, as in searching "AAAB" in a long run of A's.

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 →