Challenge - 5 Problems
Palindrome Partitioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of minimum cuts for palindrome partitioning
What is the output of the following JavaScript code that calculates the minimum cuts needed to partition the string into palindromes?
DSA Javascript
function minCut(s) {
const n = s.length;
const dp = new Array(n).fill(0);
const palindrome = Array.from({ length: n }, () => new Array(n).fill(false));
for (let end = 0; end < n; end++) {
let minCuts = end;
for (let start = 0; start <= end; start++) {
if (s[start] === s[end] && (end - start < 2 || palindrome[start + 1][end - 1])) {
palindrome[start][end] = true;
minCuts = start === 0 ? 0 : Math.min(minCuts, dp[start - 1] + 1);
}
}
dp[end] = minCuts;
}
return dp[n - 1];
}
console.log(minCut("aab"));Attempts:
2 left
💡 Hint
Think about how many cuts are needed to split "aab" into palindromes.
✗ Incorrect
The string "aab" can be split into "aa" and "b", both palindromes, with only 1 cut needed.
🧠 Conceptual
intermediateUnderstanding the palindrome table in DP
In the palindrome partitioning DP solution, what does the 2D boolean array 'palindrome' represent?
Attempts:
2 left
💡 Hint
Think about what information helps decide if a substring is palindrome quickly.
✗ Incorrect
The 'palindrome' array marks true if substring s[i..j] is palindrome, which helps avoid recomputation.
❓ Predict Output
advancedOutput of minimum cuts for a complex string
What is the output of the following code for the input string "abccbc"?
DSA Javascript
function minCut(s) {
const n = s.length;
const dp = new Array(n).fill(0);
const palindrome = Array.from({ length: n }, () => new Array(n).fill(false));
for (let end = 0; end < n; end++) {
let minCuts = end;
for (let start = 0; start <= end; start++) {
if (s[start] === s[end] && (end - start < 2 || palindrome[start + 1][end - 1])) {
palindrome[start][end] = true;
minCuts = start === 0 ? 0 : Math.min(minCuts, dp[start - 1] + 1);
}
}
dp[end] = minCuts;
}
return dp[n - 1];
}
console.log(minCut("abccbc"));Attempts:
2 left
💡 Hint
Try to find palindrome partitions with minimum cuts.
✗ Incorrect
The string "abccbc" can be partitioned as "a" | "bccb" | "c" with 2 cuts.
🔧 Debug
advancedIdentify the error in the DP palindrome partitioning code
What error will the following code produce when run?
DSA Javascript
function minCut(s) {
const n = s.length;
const dp = new Array(n).fill(0);
const palindrome = Array.from({ length: n }, () => new Array(n).fill(false));
for (let end = 0; end < n; end++) {
let minCuts = end;
for (let start = 0; start <= end; start++) {
if (s[start] === s[end] && (end - start < 2 || palindrome[start + 1][end - 1])) {
palindrome[start][end] = true;
minCuts = start === 0 ? 0 : Math.min(minCuts, dp[start - 1] + 1);
}
}
dp[end] = minCuts;
}
return dp[n - 1];
}
console.log(minCut("abc"));Attempts:
2 left
💡 Hint
Check the index used to access dp array inside the inner loop.
✗ Incorrect
The code uses dp[start] instead of dp[start - 1], causing wrong minimum cuts calculation.
🧠 Conceptual
expertTime complexity of palindrome partitioning minimum cuts DP
What is the time complexity of the dynamic programming solution for palindrome partitioning minimum cuts?
Attempts:
2 left
💡 Hint
Consider the total number of iterations in the nested loops.
✗ Incorrect
The solution uses two nested loops totaling O(n^2) iterations with O(1) work inside, resulting in quadratic time complexity.
