Data Structures & AlgorithmsHardGATE CS · TCS NQT

How many binary strings over the alphabet {a, b} of length 6 contain EXACTLY 2 occurrences of the adjacent pair "ab" (occurrences may not overlap in a way that matters here, since ab cannot overlap itself)?

  1. A. 35
  2. B. 25
  3. C. 21
  4. D. 6

Correct answer: C. 21

Track DP states by the last letter and the count so far: appending b after a adds one occurrence. Counting strings that merely contain 'ab' at least once is a different (larger) count.

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

Exams that ask this topic