132. Palindrome Partitioning II
HardSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum number of cuts to partition a string into palindromic substrings. This is an optimization problem, which strongly suggests using dynamic programming. For a string of length n, we can make at most n-1 cuts (by partitioning it into n single-character substrings). Our goal is to find the minimum possible number of cuts.
Solution
Palindrome Partitioning II in Python
class Solution:
def minCut(self, s: str) -> int:
n = len(s)
if n <= 1:
return 0
# dp[i] will store the minimum cuts for the prefix s[0...i]
# Initialize dp[i] to the worst case: i cuts for s[0...i]
dp = list(range(n))
# Iterate through all possible palindrome centers
for i in range(n):
# Check for odd length palindromes centered at i
l, r = i, i
while l >= 0 and r < n and s[l] == s[r]:
if l == 0:
# If s[0...r] is a palindrome, it needs 0 cuts.
dp[r] = 0
else:
# Otherwise, cuts = 1 + cuts for s[0...l-1]
dp[r] = min(dp[r], dp[l - 1] + 1)
l -= 1
r += 1
# Check for even length palindromes centered at i and i+1
l, r = i, i + 1
while l >= 0 and r < n and s[l] == s[r]:
if l == 0:
dp[r] = 0
else:
dp[r] = min(dp[r], dp[l - 1] + 1)
l -= 1
r += 1
return dp[n - 1]
Palindrome Partitioning II in Java
class Solution {
public int minCut(String s) {
int n = s.length();
if (n <= 1) {
return 0;
}
// dp[i] stores the minimum cuts for the prefix s[0...i]
int[] dp = new int[n];
// Initialize dp[i] to the worst case: i cuts for s[0...i]
for (int i = 0; i < n; i++) {
dp[i] = i;
}
// Iterate through all possible palindrome centers
for (int i = 0; i < n; i++) {
// Check for odd length palindromes centered at i
expandFromCenter(s, i, i, dp);
// Check for even length palindromes centered at i and i+1
expandFromCenter(s, i, i + 1, dp);
}
return dp[n - 1];
}
private void expandFromCenter(String s, int l, int r, int[] dp) {
while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
if (l == 0) {
// If s[0...r] is a palindrome, it needs 0 cuts.
dp[r] = 0;
} else {
// Otherwise, we can cut before s[l].
// Cuts = 1 (for the new cut) + cuts for s[0...l-1] (which is dp[l-1])
dp[r] = Math.min(dp[r], dp[l - 1] + 1);
}
l--;
r++;
}
}
}
Palindrome Partitioning II in C++
class Solution {
public:
int minCut(string s) {
int n = s.length();
if (n <= 1) {
return 0;
}
// dp[i] stores the minimum cuts for the prefix s[0...i]
vector<int> dp(n);
// Initialize dp[i] to the worst case: i cuts for s[0...i]
// e.g., for "abc", s[0..2], the worst case is "a|b|c", needing 2 cuts.
for (int i = 0; i < n; ++i) {
dp[i] = i;
}
// Iterate through all possible palindrome centers
for (int i = 0; i < n; ++i) {
// Check for odd length palindromes centered at i
// Example: "aba" centered at 'b' (i=1)
for (int l = i, r = i; l >= 0 && r < n && s[l] == s[r]; --l, ++r) {
if (l == 0) {
// If s[0...r] is a palindrome, it needs 0 cuts.
dp[r] = 0;
} else {
// Otherwise, we can cut before s[l].
// Cuts = 1 (for the new cut) + cuts for s[0...l-1] (which is dp[l-1])
dp[r] = min(dp[r], dp[l - 1] + 1);
}
}
// Check for even length palindromes centered at i and i+1
// Example: "abba" centered between 'b' and 'b' (i=1)
for (int l = i, r = i + 1; l >= 0 && r < n && s[l] == s[r]; --l, ++r) {
if (l == 0) {
dp[r] = 0;
} else {
dp[r] = min(dp[r], dp[l - 1] + 1);
}
}
}
return dp[n - 1];
}
};
Palindrome Partitioning II in TypeScript
function minCut(s: string): number {
const n = s.length;
if (n <= 1) {
return 0;
}
// dp[i] will store the minimum cuts for the prefix s[0...i]
const dp: number[] = new Array(n);
// Initialize dp[i] to the worst case: i cuts for s[0...i]
for (let i = 0; i < n; i++) {
dp[i] = i;
}
const expandFromCenter = (l: number, r: number) => {
while (l >= 0 && r < n && s[l] === s[r]) {
if (l === 0) {
// If s[0...r] is a palindrome, it needs 0 cuts.
dp[r] = 0;
} else {
// Otherwise, cuts = 1 + cuts for s[0...l-1]
dp[r] = Math.min(dp[r], dp[l - 1] + 1);
}
l--;
r++;
}
};
// Iterate through all possible palindrome centers
for (let i = 0; i < n; i++) {
// Check for odd length palindromes centered at i
expandFromCenter(i, i);
// Check for even length palindromes centered at i and i+1
expandFromCenter(i, i + 1);
}
return dp[n - 1];
}
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.