Data Structures & AlgorithmsHardGATE CS · Codeforces Div2 D

What is the minimum number of pieces needed to split "abacdcbaxyzyx" into contiguous pieces where every piece is a palindrome?

  1. A. 6
  2. B. 5
  3. C. 4
  4. D. 8

Correct answer: B. 5

dp[j] = 1 + min dp[i] over palindromes s[i..j−1]: this is the minimum number of pieces, which is the minimum cuts plus one. Greedily taking the longest palindrome from the left can fail, because the longest first piece may leave an awkward remainder.

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

Exams that ask this topic