Data Structures & AlgorithmsModerateGATE CS · ICPC regional
What is the Z-function value sum over positions i = 1 to n-1 of the string abababab?
- A. 13
- B. 14
- C. 12
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many index pairs i < j in [1, 2, 3, 4, 5, 6] satisfy that a[i] times a[j] equals a[i] plus a[j]?
- Which data structure supports inserting values and reporting the k-th smallest value in O(log n) time each?
- Which algorithm counts occurrences of a pattern p inside a text t in O(|t| + |p|) time?
- Which algorithm finds the longest palindromic substring of a string in linear time?
- Which technique detects whether an array contains a non-empty subarray with sum zero in O(n) expected time?
- An array of 200000 integers may contain negative values, and you must count its contiguous subarrays whose sum equals K in near-linear…