Data Structures & AlgorithmsModerateICPC regional · string round

Problem: What is the time complexity of computing the Z-array of a string of length n?

  1. A. O(n^2)
  2. B. O(n log n)
  3. C. O(n sqrt n)
  4. D. O(n)

Correct answer: D. O(n)

The Z-algorithm reuses the rightmost matched window, so every character is compared a constant number of times.

More Coding Interview Problems questions

Exams that ask this topic