Data Structures & AlgorithmsModerateICPC regional · Amazon SDE OA

How many times does "ab" occur as a substring of the string "aab" repeated three times?

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

Correct answer: C. 3

Scan the length-9 string and count windows of length 2 equal to ab. The boundary between repeats is 'b' followed by 'a', which is not ab, so only the internal occurrences count.

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

Exams that ask this topic