Data Structures & AlgorithmsHardTCS NQT · Codeforces Div2 B

In "abababaabab", what is the maximum number of NON-OVERLAPPING occurrences of "aba" that can be chosen (left to right, each occurrence taking its own letters)?

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

Correct answer: B. 3

Scan left to right and, on each match, jump past it: this greedy is optimal because the earliest-ending occurrence leaves the most room. Counting all overlapping occurrences overcounts for this pattern.

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

Exams that ask this topic