Data Structures & AlgorithmsHardTCS NQT · Product Company SDE

Problem: Count the strings of length 4 over the alphabet {a, b, c} that contain no two adjacent equal characters and do not contain the substring "ab".

  1. A. 13
  2. B. 24
  3. C. 55
  4. D. 8

Correct answer: A. 13

Brute force over the 81 strings (or a DP on the last letter) finds 13. Ignoring the 'ab' ban gives 3 · 2³ = 24, ignoring the adjacency rule gives 55, and also banning 'ba' gives 8.

More Coding Interview Problems questions

Exams that ask this topic