🎙 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 6 of 10

What does this print? (code)

What does this print?

text, pat = "abababa", "aba"
print([i for i in range(len(text) - len(pat) + 1) if text[i:i + len(pat)] == pat])
  1. A.[0, 4]
  2. B.[0]
  3. C.[0, 2, 4]
  4. D.[0, 1, 2, 3, 4]
Answer & explanation

Correct answer: [0, 2, 4]

Matches may overlap: "aba" starts at 0, 2 and 4.

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 →