GATE Computer ScienceVery hardGATE CS (PYQ pattern)

For the string s = abaababaabaab (13 characters), a number p (1 ≤ p ≤ 13) is called a period if s[i] = s[i + p] for every valid i (p = 13 counts trivially). How many periods does s have?

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

Correct answer: C. 3

Periods correspond to borders: p is a period iff s has a border of length 13 − p. The borders of s are abaab (5), ab (2) and the empty string (follow the KMP failure chain 5 → 2 → 0), so the periods are 8, 11 and 13. Forgetting the trivial period gives 2; taking only n − π[n−1] = 8 gives 1.

More Algorithms questions

Exams that ask this topic