Longest Chunked Palindrome Decomposition

You are given a string text. You should split it into k substrings (subtext1, subtext2, ..., subtextk) such that:

  • subtexti is a non-empty string.
  • The concatenation of all the substrings is equal to text (i.e., subtext1 + subtext2 + ... + subtextk == text).
  • subtexti == subtext{k - i + 1} for all valid values of i (i.e., 1 <= i <= k).

Return the largest possible value of k.

Example 1
Inputtext = "ghiabcdefhelloadamhelloabcdefghi"
Output7
We can split the string on "(ghi)(abcdef)(hello)(adam)(hello)(abcdef)(ghi)".
Example 2
Inputtext = "merchant"
Output1
We can split the string on "(merchant)".

Constraints

  • 1 <= text.length <= 1000
  • text consists only of lowercase English characters.

Asked at 1 companies

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate