Data Structures & AlgorithmsHardGATE CS · TCS NQT

How many strings of length 5 over the alphabet {a, b, c} do NOT contain the substring "abc"?

  1. A. 220
  2. B. 216
  3. C. 75
  4. D. 217

Correct answer: B. 216

A string contains 'abc' at a position i exactly when the three letters starting there match; counting strings with the substring is inclusion–exclusion over positions. Subtracting only the strings where 'abc' starts at the first position undercounts.

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

Exams that ask this topic