GATE Computer ScienceModerateGATE CS (PYQ pattern)

How many strings of length 6 over {a, b} contain both ab and ba as substrings?

  1. A. 50
  2. B. 57
  3. C. 52
  4. D. 48

Correct answer: C. 52

Inclusion–exclusion on the complements: strings with no ab are b*a* (7 of length 6), with no ba are a*b* (7), and with neither are aaaaaa and bbbbbb (2). 64 − 7 − 7 + 2 = 52.

More Theory of Computation questions

Exams that ask this topic