Data Structures & AlgorithmsVery hard

How many index triples i < j < k in "abcdeacace" spell the subsequence 'ace' (characters need not be adjacent)?

  1. A. 1
  2. B. 18
  3. C. 7
  4. D. 6

Correct answer: C. 7

Count the index triples in order, which is a DP over the pattern: the number of ways to match each prefix of the pattern. Counting contiguous occurrences, multiplying letter counts ignores order, and counting pairs drops the last letter.

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

Exams that ask this topic