Data Structures & AlgorithmsHardProduct-company MCQ pattern

What is the KMP failure (LPS) array for the pattern AABAACAABAA?

  1. A. 0 1 0 0 1 0 1 2 3 4 5
  2. B. 0 1 0 1 2 0 1 2 3 4 5
  3. C. 0 1 0 1 2 1 1 2 3 4 5
  4. D. 0 1 0 1 2 0 1 2 3 4 4

Correct answer: B. 0 1 0 1 2 0 1 2 3 4 5

LPS[i] is the length of the longest proper prefix of P[0..i] that is also its suffix. After C (no match, 0) the string AABAA repeats from the start, giving 1, 2, 3, 4, 5 for the last five positions.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic