Longest Chunked Palindrome Decomposition in python

The Longest Chunked Palindrome Decomposition problem asks us to find the maximum number of chunks we can split a string into, where the chunks form a palindromic pattern. This means the first chunk equals the last chunk, the second equals the second-to-last, and so on.

For example, given the string "antaprezatepzapreanta", we can decompose it as "(a)(nt)(a)(pre)(za)(tpe)(za)(pre)(a)(nt)(a)" which gives us 11 chunks in palindromic order.

Algorithm

The approach uses two pointers moving from both ends toward the center ?

  • Use two pointers: start at the beginning and end at the end
  • Build chunks from both sides simultaneously using temp1 and temp2
  • When chunks match, increment count by 2 and reset the temporary strings
  • Handle the middle character for odd-length strings
  • Add 1 to count if there are remaining unmatched characters at the end

Implementation

class Solution:
    def longestDecomposition(self, text):
        start = 0
        end = len(text) - 1
        temp1 = ""
        temp2 = ""
        # Start with 1 if odd length (middle character), 0 if even
        ans = 1 if len(text) % 2 == 1 else 0
        
        while start < end:
            temp1 += text[start]
            temp2 = text[end] + temp2
            
            if temp1 == temp2:
                temp1 = temp2 = ""
                ans += 2
            
            start += 1
            end -= 1
        
        # If even length and leftover characters, add 1
        if len(text) % 2 == 0 and (temp1 or temp2):
            ans += 1
        
        return ans

# Test the solution
solution = Solution()
print(solution.longestDecomposition("antaprezatepzapreanta"))
11

How It Works

Let's trace through "antaprezatepzapreanta" ?

def trace_decomposition(text):
    start = 0
    end = len(text) - 1
    temp1 = ""
    temp2 = ""
    ans = 1 if len(text) % 2 == 1 else 0
    chunks = []
    
    while start < end:
        temp1 += text[start]
        temp2 = text[end] + temp2
        
        if temp1 == temp2:
            chunks.append(temp1)
            temp1 = temp2 = ""
            ans += 2
        
        start += 1
        end -= 1
    
    if len(text) % 2 == 0 and (temp1 or temp2):
        ans += 1
    
    return ans, chunks

result, chunks = trace_decomposition("antaprezatepzapreanta")
print(f"Result: {result}")
print(f"Matching chunks: {chunks}")
Result: 11
Matching chunks: ['a', 'nt', 'a', 'pre', 'za']

Example with Different Input

solution = Solution()

test_cases = [
    "ghiabcdefhelloadamhelloabcdefghi",
    "merchant",
    "aaa"
]

for text in test_cases:
    result = solution.longestDecomposition(text)
    print(f"'{text}' ? {result} chunks")
'ghiabcdefhelloadamhelloabcdefghi' ? 7
'merchant' ? 1
'aaa' ? 3

Conclusion

The algorithm efficiently finds the maximum palindromic decomposition by using two pointers and greedy matching. The time complexity is O(n²) in the worst case, where n is the string length, due to string comparisons.

Updated on: 2026-03-25T08:43:40+05:30

382 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements