Data Structures & AlgorithmsHardGATE CS · TCS NQT

How many strings of length 4 over the alphabet {a, b} contain the letter a somewhere before a b (that is, contain 'a' followed later by 'b' as a subsequence)?

  1. A. 12
  2. B. {C+7}
  3. C. 11
  4. D. 14

Correct answer: C. 11

Strings with no a before any b are exactly the strings of the form b...ba...a, and there are 5 of those among 16, so the answer is 16 - 5 = 11.

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

Exams that ask this topic