Data Structures & AlgorithmsVery hard

How many index triples i < j < k in "abacaba" give a palindromic subsequence S[i]S[j]S[k] of length 3 (that is, S[i] = S[k])?

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

Correct answer: D. 17

A length-3 subsequence is a palindrome exactly when its first and last characters match, and the middle one is free. Requiring the middle character to match too, counting contiguous palindromes, or counting pairs answers different questions.

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

Exams that ask this topic