Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

Let L be the length of the longest common subsequence of "ABCBDAB" and "BDCABA". How many different index-selections in "ABCBDAB" give a common subsequence of length L (each selection is a set of positions, and two selections with the same letters but different positions count separately)? Count index sets of the first string that spell an LCS which also occurs in the second string.

  1. A. 4
  2. B. 7
  3. C. 1
  4. D. 5

Correct answer: A. 4

Track both the length and the number of ways for each prefix pair: when letters match, extend the diagonal count; when they do not, add the counts of the tied neighbours. Reporting only the LCS length answers a different question.

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

Exams that ask this topic