Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT

How many index triples i < j < k in "abacbab" spell a palindrome of length 3, meaning s[i] = s[k]? (The middle letter is unrestricted.)

  1. A. 17
  2. B. 12
  3. C. 35
  4. D. 14

Correct answer: D. 14

A length-3 palindromic subsequence only needs the outer letters to match, so for each pair (i, k) with s[i] = s[k] there are k − i − 1 choices of middle letter. Counting only consecutive triples or all triples ignores that structure.

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

Exams that ask this topic