GATE Computer ScienceVery hardGATE CS (PYQ pattern)

How many DIFFERENT non-empty strings occur as subsequences of BANANAB (subsequences need not be contiguous; equal strings from different index sets count once)?

  1. A. 127
  2. B. 78
  3. C. 79
  4. D. 63

Correct answer: B. 78

Use the DP d(i) = 2·d(i−1) − d(last(i) − 1), where last(i) is the previous occurrence of the same letter (this removes strings counted twice), with d(0) = 1 for the empty string. Over B, A, N, A, N, A, B this gives 2, 4, 8, 14, 24, 40, 79 (79 includes the empty string), so 78 non-empty. 127 = 2⁷ − 1 counts index sets, not strings.

More Algorithms questions

Exams that ask this topic