Description
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
Example 1:
Input: s = "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.
Example 2:
Input: s = "a"
Output: 0
Example 3:
Input: s = "ab"
Output: 1
Constraints:
1 <= s.length <= 2000sconsists of lowercase English letters only.
Primary Approach: Expand Around Center DP ( Time, Space)
Intuition
To solve 132. Palindrome Partitioning II, define cuts[i] as the minimum cuts required to partition the prefix s[0 ... i] into palindromic substrings:
- Worst-Case Initialization: Initialize
cuts[i] = ibecause cutting every single character requires at mosticuts for a prefix of lengthi + 1. - Expand Around Center: Every palindrome has a center. Iterating through each index
midas a potential palindrome center allows us to expand outward to discover all palindromic substringss[left ... right]:- Odd Length Palindromes: Center at
(mid, mid). - Even Length Palindromes: Center at
(mid, mid + 1).
- Odd Length Palindromes: Center at
- Transition: Whenever
s[left ... right]is a valid palindrome:- If
left == 0, the whole prefixs[0 ... right]is a palindrome, socuts[right] = 0. - Otherwise,
cuts[right] = min(cuts[right], cuts[left - 1] + 1).
- If
class Solution {
public int minCut(String s) {
int n = s.length();
int[] cuts = new int[n];
// Max possible cuts for s[0...i] is i (cutting into individual characters)
for (int i = 0; i < n; i++) {
cuts[i] = i;
}
for (int mid = 0; mid < n; mid++) {
// Odd length palindromes (centered at mid)
expandAndCalculate(s, mid, mid, cuts);
// Even length palindromes (centered at mid, mid + 1)
expandAndCalculate(s, mid, mid + 1, cuts);
}
return cuts[n - 1];
}
private void expandAndCalculate(String s, int left, int right, int[] cuts) {
int n = s.length();
while (left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
if (left == 0) {
// Entire prefix s[0...right] is a palindrome
cuts[right] = 0;
} else {
cuts[right] = Math.min(cuts[right], cuts[left - 1] + 1);
}
left--;
right++;
}
}
}
Complexity
- Time Complexity: — Expanding around all possible centers takes time per center.
- Space Complexity: auxiliary space — Only uses a single 1D array
cutsof size .
Alternative Approach: 2D Palindrome Table + 1D DP ( Time, Space)
Intuition
Split the subproblems into two clear steps:
- Precompute Palindromes: Use a 2D
booleantableisPal[j][i]whereisPal[j][i] = trueifs[j ... i]is a palindrome. A substrings[j ... i]is a palindrome ifs.charAt(j) == s.charAt(i)AND (i - j <= 2ORisPal[j + 1][i - 1]istrue). - Compute Minimum Cuts: Iterate
ifrom to . IfisPal[0][i]is true,cuts[i] = 0. Otherwise, test all split pointsjfrom to :
class Solution {
public int minCut(String s) {
int n = s.length();
boolean[][] isPal = new boolean[n][n];
int[] cuts = new int[n];
for (int i = 0; i < n; i++) {
int minCuts = i; // Max cuts needed is i
for (int j = 0; j <= i; j++) {
if (s.charAt(j) == s.charAt(i) && (i - j <= 2 || isPal[j + 1][i - 1])) {
isPal[j][i] = true;
if (j == 0) {
minCuts = 0;
} else {
minCuts = Math.min(minCuts, cuts[j - 1] + 1);
}
}
}
cuts[i] = minCuts;
}
return cuts[n - 1];
}
}
Complexity
- Time Complexity: — Nested loop over string length .
- Space Complexity: auxiliary space — Required for the 2D matrix
isPal.
Key Interview Discussion Points
- Space Advantage of Expand Around Center: The Expand Around Center approach computes palindrome validity dynamically without building a full matrix, cutting auxiliary space down to .
- Comparison with Palindrome Partitioning I: While 131. Palindrome Partitioning asks for all possible valid partitions (requiring backtracking), this problem only asks for the minimum number of cuts, making Dynamic Programming optimal.
Easy Memory Rule
“Expand center If
s[left ... right]is palindromecuts[right] = min(cuts[right], cuts[left - 1] + 1)!”