Data Structures & AlgorithmsHardTCS NQT Digital · Infosys

Problem: How many strings of length 3 over the alphabet {a, b, c} contain no two consecutive a's?

  1. A. 27
  2. B. 22
  3. C. 21
  4. D. 5

Correct answer: B. 22

Strings containing "aa": aaa, aab, aac, baa, caa = 5, so 27 − 5 = 22. 27 removes nothing, counting aaa twice gives 21, and 5 counts the bad strings.

More Coding Interview Problems questions

Exams that ask this topic