Longest Chunked Palindrome Decomposition - Problem
You are given a string text. You should split it to k substrings (subtext₁, subtext₂, ..., subtextₖ) such that:
subtextᵢis a non-empty string.- The concatenation of all the substrings is equal to
text(i.e.,subtext₁ + subtext₂ + ... + subtextₖ == text). subtextᵢ == subtextₖ₋ᵢ₊₁for all valid values ofi(i.e.,1 <= i <= k).
Return the largest possible value of k.
Input & Output
Example 1 — Palindromic Chunks
$
Input:
text = "ghiabcdefhelloadefghiabcdef"
›
Output:
1
💡 Note:
No palindromic decomposition is possible for this string. No prefix matches any suffix, so the entire string forms one chunk.
Example 2 — Single Character
$
Input:
text = "aa"
›
Output:
2
💡 Note:
Split into (a)(a) - two matching single character chunks
Example 3 — No Splits
$
Input:
text = "abc"
›
Output:
1
💡 Note:
No way to split into palindromic chunks, so the whole string is one chunk
Constraints
- 1 ≤ text.length ≤ 1000
- text consists of lowercase English letters only
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code