Data Structures & AlgorithmsHardProduct Company SDE · Infosys OA

Problem: A string s = "abacaba" is given. How many of its substrings are palindromes of odd length (counting index ranges, so repeated strings at different positions count separately)?

  1. A. 7
  2. B. 16
  3. C. 5
  4. D. 12

Correct answer: D. 12

Expanding around the 7 single-letter centres finds 7 + 3 + 1 + 1 = 12 odd palindromes (letters, aba/aca/aba, bacab, abacaba). Counting distinct strings gives 7, counting all odd-length substrings gives 16, and skipping the single letters gives 5.

More Coding Interview Problems questions

Exams that ask this topic