Data Structures & AlgorithmsVery hardICPC regional · Amazon SDE OA

Using the Z-function on "ab#abababcab", how many occurrences of the pattern "ab" are found in the text "abababcab"?

  1. A. 5
  2. B. 3
  3. C. 4
  4. D. 6

Correct answer: C. 4

Concatenate pattern, a separator, and text, then count positions where the Z-value reaches the pattern length. The separator stops the Z-values from crossing into the text prefix.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic