Data Structures & AlgorithmsVery hard

When counting subsequences 'ace' in a string, why does a DP over the pattern prefixes avoid double counting?

  1. A. Subsequences are always counted twice by definition.
  2. B. Only the first letter is used in the DP.
  3. C. The DP uses sorting so duplicate indices collapse.
  4. D. Each index is matched to a unique pattern position in increasing order, so each index triple is counted exactly once.

Correct answer: D. Each index is matched to a unique pattern position in increasing order, so each index triple is counted exactly once.

Processing the string left to right and extending the count of matched prefixes gives each index tuple once, because positions are consumed in increasing order. This is the standard subsequence-counting recurrence.

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

Exams that ask this topic