Java String Program · S65
Palindrome Partitioning with Minimum Cuts in Java
Cut the string so that every piece is a palindrome, using as few cuts as possible. For each prefix, try every palindrome that ends at its last character.
Problem Statement
Given a string, return the minimum number of cuts needed so that every resulting piece is a palindrome.
A string that is already a palindrome needs 0 cuts; a string of n different characters needs n - 1.
Input/Output Examples
"racecarx"Output1Explanation: racecar | x."noonabbad"Output2Explanation: noon | abba | d."abba"Output0Explanation: the whole string is a palindrome.Visual Explanation
- Prefix "noon"piecesnoonpalindrome from 0 cuts[3] = 0
- Prefix …"abba"piecesnoonabbapalindrome 4..7 cuts[7] = cuts[3] + 1 = 1
- Prefix …"d"piecesnoonabbadonly "d" ends at 8 cuts[8] = cuts[7] + 1 = 2
- Done2 cuts
"noonabbad": cuts[j] is the fewest cuts for the prefix ending at j. Each palindrome s[i..j] offers cuts[i - 1] + 1, or 0 when it starts at index 0.The last piece of any valid partition is a palindrome ending at the last character. Trying every such palindrome and adding one cut to the best answer for the prefix before it gives the optimum.
Approaches
Both approaches compute cuts[j] for every prefix. They differ in how they find the palindromes ending at each position.
Approach 1: Cuts DP With a Palindrome Table (Recommended)
Idea: for each end j, check every start i; when s[i..j] is a palindrome, update cuts[j].
- Start with
cuts[j] = j(cut after every character). - Mark
pal[i][j]whens[i] == s[j]and the inside is a palindrome. - For each palindrome, set
cuts[j] = min(cuts[j], i == 0 ? 0 : cuts[i - 1] + 1). - Return
cuts[n - 1].
Time: O(n²) · Space: O(n²).
Approach 2: Expand Around Center
Idea: expand around every centre; every palindrome s[l..r] found offers cuts[r] = cuts[l - 1] + 1.
- Initialise
cuts[i] = i. - For each centre, expand while the ends match.
- For each palindrome
s[l..r], relaxcuts[r].
Time: O(n²) · Space: O(n).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Cuts DP With a Palindrome Table (Recommended)
The palindrome table is filled in the same loop, because pal[i + 1][j - 1] belongs to an earlier column.
public class MinCutPalTable {
// cuts[j] = fewest cuts for s[0..j]; pal[i][j] marks palindromic substrings
static int minCut(String s) {
int n = s.length();
if (n == 0) {
return 0;
}
boolean[][] pal = new boolean[n][n];
int[] cuts = new int[n];
for (int j = 0; j < n; j++) {
cuts[j] = j; // worst case: cut after every character
for (int i = 0; i <= j; i++) {
if (s.charAt(i) == s.charAt(j) && (j - i < 2 || pal[i + 1][j - 1])) {
pal[i][j] = true;
cuts[j] = Math.min(cuts[j], i == 0 ? 0 : cuts[i - 1] + 1);
}
}
}
return cuts[n - 1];
}
public static void main(String[] args) {
String[] inputs = {"racecarx", "noonabbad", "abba", "abcd"};
for (String s : inputs) {
System.out.println("\"" + s + "\" -> " + minCut(s));
}
}
}
"racecarx" -> 1 "noonabbad" -> 2 "abba" -> 0 "abcd" -> 3
Time complexity: O(n²) · Space complexity: O(n²).
Approach 2: Expand Around Center
When cuts[r] is updated, cuts[l - 1] is already final, because every palindrome ending at l - 1 has a centre to the left of the current one.
public class MinCutExpand {
// Expands around every centre; each palindrome s[l..r] offers cuts[r] = cuts[l - 1] + 1
static int minCut(String s) {
int n = s.length();
if (n == 0) {
return 0;
}
int[] cuts = new int[n];
for (int i = 0; i < n; i++) {
cuts[i] = i;
}
for (int c = 0; c < n; c++) {
relax(s, cuts, c, c); // odd-length palindromes
relax(s, cuts, c, c + 1); // even-length palindromes
}
return cuts[n - 1];
}
private static void relax(String s, int[] cuts, int l, int r) {
while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
cuts[r] = Math.min(cuts[r], l == 0 ? 0 : cuts[l - 1] + 1);
l--;
r++;
}
}
public static void main(String[] args) {
String[] inputs = {"racecarx", "noonabbad", "abba", "abcd"};
for (String s : inputs) {
System.out.println("\"" + s + "\" -> " + minCut(s));
}
}
}
"racecarx" -> 1 "noonabbad" -> 2 "abba" -> 0 "abcd" -> 3
Time complexity: O(n²) · Space complexity: O(n).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Cuts DP With a Palindrome Table | O(n²) | O(n²) |
| Expand Around Center | O(n²) | O(n) |
Here n is the length of the string. Both approaches examine O(n²) palindromes; the table version also stores n² booleans.
Interview Tips and Recommended Approach
- Avoid plain recursion. Trying every partition is exponential.
- Separate the two questions. "Is this piece a palindrome?" and "what is the fewest cuts?" each have their own table.
- Initialise with the worst case.
cuts[j] = jis always achievable. - Know the sibling problem. Listing all palindrome partitions needs backtracking and has exponential output.
Recommended Approach: Cuts DP With a Palindrome Table
Why: it combines the palindrome check and the cut count in one clear pass, and every step can be traced by hand on small inputs.