GATE Computer ScienceHardGATE CS (PYQ pattern)

The grammar S → aSbS | ε generates 42 strings of length 10. In how many of them does no prefix contain more than two a's in excess of b's (that is, n_a − n_b ≤ 2 for every prefix)?

  1. A. 14
  2. B. 32
  3. C. 16
  4. D. 8

Correct answer: C. 16

The grammar generates Dyck words (a = up, b = down). Height ≤ 2: such a word is a sequence of blocks a·(ab)ᵏ·b, k ≥ 0, each of length 2k + 2. With 5 a-b pairs this is the number of compositions of 5 into positive parts, 2⁴ = 16.

More Theory of Computation questions

Exams that ask this topic