Data Structures & AlgorithmsModerateGATE CS · ICPC regional

What is the Z-function value sum over positions i = 1 to n-1 of the string abababab?

  1. A. 13
  2. B. 14
  3. C. 12
  4. D. 16

Correct answer: C. 12

Z[i] is the longest common prefix of the string and its suffix from i. Odd positions give Z = 0, while even positions give the remaining length (6, 4 and 2 at positions 2, 4 and 6), so the sum is 12.

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

Exams that ask this topic