Data Structures & AlgorithmsVery hardGATE CS · TCS NQT

How many length-2 substrings of "abbaabab" are either "ab" or "ba"?

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

Correct answer: B. 5

Every adjacent pair with two different letters is either ab or ba here, and the pairs bb and aa are excluded. Counting differing adjacent pairs gives the same number.

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

Exams that ask this topic