Data Structures & AlgorithmsHardICPC regional · string round

Problem: Which statement about the Z-function of a string of length n is correct?

  1. A. It takes O(n^2) time because each position is compared to all others
  2. B. It can be computed in O(n) time and gives, for each i, the longest prefix matched starting at i
  3. C. It only works on strings over the alphabet {0, 1}
  4. D. It returns only the number of distinct substrings

Correct answer: B. It can be computed in O(n) time and gives, for each i, the longest prefix matched starting at i

The Z-algorithm reuses a window of previous matches, so it runs in linear time and gives the longest common prefix of each suffix with the whole string.

More Coding Interview Problems questions

Exams that ask this topic