1251. Longest Chunked Palindrome Decomposition
My accepted Python solution to LeetCode problem 1251, Longest Chunked Palindrome Decomposition, running in 0ms.
- Difficulty: Hard
- Python
- Runtime 0ms
- Memory 17MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 0ms, memory 17MB, accepted 2025-12-29.
class Solution:
def longestDecomposition(self, text: str) -> int:
# Greedy approach: try to find smallest matching prefix and suffix
n = len(text)
left = 0
right = n - 1
count = 0
left_str = ""
right_str = ""
while left < right:
left_str += text[left]
right_str = text[right] + right_str
if left_str == right_str:
count += 2
left_str = ""
right_str = ""
left += 1
right -= 1
# Handle middle part
if left == right or left_str:
count += 1
return count