Data Structures & AlgorithmsHard

To count distinct subsequences of a string with repeated letters, which strategy avoids double counting?

  1. A. Count all index subsets and divide by the number of repeated letters.
  2. B. Count only contiguous substrings.
  3. C. Process characters left to right and track the number of distinct subsequences, subtracting the count at the last occurrence of each character.
  4. D. Sort the string before counting.

Correct answer: C. Process characters left to right and track the number of distinct subsequences, subtracting the count at the last occurrence of each character.

Adding a character doubles the distinct subsequences, except those already produced by its previous occurrence, which must be subtracted. This recurrence counts distinct strings directly.

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

Exams that ask this topic