Data Structures & AlgorithmsHardGATE CS · TCS NQT

How many times does the pattern "aaa" occur in the text "aaaabaaaaab", counting OVERLAPPING occurrences (occurrences may share letters)?

  1. A. 2
  2. B. 5
  3. C. 7
  4. D. 8

Correct answer: B. 5

After a match you fall back through the prefix function instead of restarting, so overlapping matches are still found. Non-overlapping counting (jumping past each match) gives a smaller number when occurrences can share letters.

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

Exams that ask this topic