Data Structures & AlgorithmsVery hardICPC regional · Amazon SDE OA
Using the Z-function on "ab#abababcab", how many occurrences of the pattern "ab" are found in the text "abababcab"?
- A. 5
- B. 3
- C. 4
- D. 6
Correct answer: C. 4
Concatenate pattern, a separator, and text, then count positions where the Z-value reaches the pattern length. The separator stops the Z-values from crossing into the text prefix.
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
- For "abacabad", how many indices i (0-indexed) have prefix-function value pi[i] equal to 0?
- For the string "abcdeca", take all length-2 substrings and hash each with h(xy) = (ord(x) + 2ord(y)) mod 5. How many index pairs i < j of…
- How many integers from 1 to 100 inclusive have exactly two 1-bits in their binary representation?
- What is the XOR of the elements at indices 2 through 6 (inclusive) of [5, 8, 1, 9, 4, 3, 7, 2]?
- What is the minimum number of bit flips (in a and b combined) to make a OR b equal to c, for a = 2, b = 6, c = 5? (Flip single bits of a…
- How many non-empty subsets of [1, 2, 3, 4, 5] have XOR of their elements equal to 0?