How to split a string into as few palindromes as possible?
Codemia Knowledge Hub ·
Introduction
This problem asks for the smallest number of palindromic substrings whose concatenation is the original string. It is a classic dynamic programming problem because many overlapping substrings need to be checked repeatedly. A correct solution is much more efficient than trying every possible partition.
Problem Description
Given a string s, split it into contiguous pieces so that every piece is a palindrome and the number of pieces is minimized.
Examples:
abacabaneeds only1part because the whole string is a palindrome.bananacan be split asb | anana, so the minimum number of parts is2.
If you are solving the related "minimum cuts" variant, the answer is simply:
minimum parts - 1
Dynamic Programming Idea
There are two subproblems:
- Determine whether
s[start:end+1]is a palindrome. - Compute the minimum number of palindromic parts needed for each prefix of the string.
Define:
is_pal[start][end]asTrueif the substring is a palindromeparts[end]as the minimum number of palindromic parts needed fors[0:end+1]
For each end, try every start <= end. If s[start:end+1] is a palindrome, then:
- if
start == 0, the candidate answer is1 - otherwise the candidate answer is
parts[start - 1] + 1
Take the minimum over all such candidates.
Why This Works
If a partition ends with a palindromic suffix, then the part before that suffix must itself be an optimal solution for the prefix. That is exactly the structure dynamic programming needs:
- optimal substructure
- overlapping subproblems
Without memoization or DP, a brute-force search over all partitions becomes exponential.
Python Implementation
This implementation computes both the minimum number of parts and one optimal partition.
Complexity
The standard DP solution runs in O(n^2) time:
- there are
O(n^2)substrings - each
start, endpair is processed once
The straightforward version uses O(n^2) space because of the palindrome table.
Worked Example
Consider banana.
Important palindromic substrings include:
bananaanana
The best partition is:
b | anana
So:
- minimum parts =
2 - minimum cuts =
1
That is much better than naive partitions like b | ana | n | a.
Common Pitfalls
- Confusing "minimum parts" with "minimum cuts".
- Forgetting that a whole string can itself be a palindrome.
- Rechecking palindromes from scratch inside the DP loop, which can accidentally turn the solution into
O(n^3). - Using the wrong example result for
banana. The optimal answer is2parts, not4.
Summary Table
| Point | Details | |
| Problem | Minimize the number of palindromic substrings | |
| Core technique | Dynamic programming | |
| Palindrome helper | is_pal[start][end] | |
| Main state | parts[end] | |
| Time complexity | O(n^2) | |
| Space complexity | O(n^2) | |
| Example | banana becomes `b | anana` |
Related reading
- How to spot a greedy algorithm?
- How to subsample a 2D polygon?
- How to tell if an array is a permutation in On?
- How to tell if greedy algorithm suffices for finding minimum coin change?
- How to test a hash function?
- How to test if one string is a subsequence of another?
- How to think in recursive way?
- How to trace the path in a Breadth-First Search?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.