Data Structures & AlgorithmsHardICPC regional · string round
Problem: Which statement about the Z-function of a string of length n is correct?
- A. It takes O(n^2) time because each position is compared to all others
- B. It can be computed in O(n) time and gives, for each i, the longest prefix matched starting at i
- C. It only works on strings over the alphabet {0, 1}
- 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
- Problem: How many subarrays of [2, 3, 4, 5] contain exactly one odd number? Run the code. python a = [2, 3, 4, 5] print(sum(1 for i in…
- Problem: What is the length of the longest subarray of [1, 2, 3, 4, 5] whose sum is at most 10? Run the two-pointer code. python a = [1…
- Problem: How many pairs (i < j) of [1, 2, 3, 4, 5] have a[i] a[j] at most 10? Run the code. python a = [1, 2, 3, 4, 5] print(sum(1 for i…
- Problem: How many distinct values appear among the minima of all subarrays of [3, 1, 2]? Run the code. python a = [3, 1, 2] mins =…
- Problem: What is the maximum value of a[i] + a[j] - (j - i) over pairs i < j of a = [1, 3, -2, 5]? Run the code. python a = [1, 3, -2, 5]…
- Problem: What is the total number of 1-bits in the binary representations of 1 through 10? Run the code. python…